         
         let (cw, ch) = cubeSize()
 
-        let nx = Int(bounds.size.width)/cw
-        let ny = Int(bounds.size.height)/ch
-
-        nextPos = UInt(arc4random_uniform(6))
+        let nx = width()/cw
+        let ny = height()/ch
+
+        nextPos = randint(5)
         lastPos = nextPos
 
         switch nextPos

        super.stopAnimation()
    }
    
    func setup() {
        numrows = min(numrows, Int(bounds.size.height)/2)
        // start at centre
        let (cw, ch) = cubeSize()
        pos = (Int(bounds.size.width)/cw/2 , numrows/2)
    }
    
    override func drawRect(rect: NSRect) {
        super.drawRect(rect)
        var bPath:NSBezierPath = NSBezierPath(rect: bounds)
        NSColor.blackColor().set()
        bPath.fill()
    }
    
    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 drawNextCube()


import ScreenSaver

class KrkklView : ScreenSaverView
{
    var numrows = 100
    var pos:     (x: Int, y: Int) = (0, 0)
    var next:    (x: Int, y: Int) = (0, 0)
    var center:  (x: Int, y: Int) = (0, 0)

    var lastPos:Int = 0
    var nextPos:Int = 0
    var reset: String = "center" // what happens when the screen border is touched: "wrap", "center" or "random"
    
    override init(frame: NSRect, isPreview: Bool) {
        super.init(frame: frame, isPreview: isPreview)
