         super.stopAnimation()
     }
     
+    func width() -> Int {
+        return Int(bounds.size.width)
+    }
+    func height() -> Int {
+        return Int(bounds.size.height)
+    }
+    func randint(n: Int) -> Int {
+        return Int(arc4random_uniform(UInt32(n)+1))
+    }
+    
     func setup() {
-        numrows = min(numrows, Int(bounds.size.height)/2)
+
+        var numrows = 100
+        numrows = min(numrows, height()/2)
+
         // start at centre
         let (cw, ch) = cubeSize()
-        pos = (Int(bounds.size.width)/cw/2 , numrows/2)
-    }
-    
-    override func drawRect(rect: NSRect) {
+        pos = (width()/cw/2 , numrows/2)
+        
+        next.x = Int(width()/cw)
+        next.y = Int(height()/ch)
+ 
+        center.x = Int(next.x/2)
+        center.y = Int(next.y/2)
+    }
+    
+    override func drawRect(rect: NSRect) 
+    {
         super.drawRect(rect)
         var bPath:NSBezierPath = NSBezierPath(rect: bounds)
         NSColor.blackColor().set()
         bPath.fill()
     }
     
-    override func animateOneFrame() {
+    override func animateOneFrame() 
+    {
         let context = window!.graphicsContext
         NSGraphicsContext.setCurrentContext(context)
         drawNextCube()
         context?.flushGraphics()
     }
     
-    func cubeSize() -> (w: Int, h: Int) {
-        let height = Int(bounds.size.height)/numrows
-        return (Int(sin(M_PI/3) * Double(height)), height)
+    func cubeSize() -> (w: Int, h: Int) 
+    {
+        let h = height()/numrows
+        return (Int(sin(M_PI/3) * Double(h)), h)
     }
     
     func drawNextCube()

        
        let (cw, ch) = cubeSize()

        let nx = Int(bounds.size.width)/cw
        let ny = Int(bounds.size.height)/ch

        nextPos = UInt(arc4random_uniform(6))
        lastPos = nextPos

        switch nextPos

            path.fill()
        }
    }
}

extension NSColor {
    
    func red()   -> CGFloat { return redComponent   }
    func green() -> CGFloat { return greenComponent }
    func blue()  -> CGFloat { return blueComponent  }
    
    func darken(factor: Float) -> NSColor {
        let f = CGFloat(factor)
        return NSColor(red: red()*f, green: green()*f, blue: blue()*f, alpha: 1)
    }
}
