 //            }
 //            else if reset == "wrap" {
             if (true) {
-                if (x < 1) {
-                    x = nx-1
+                if (pos.x < 1) {
+                    pos.x = nx-1
                 }   
-                else if (x > nx-1)
+                else if (pos.x > nx-1)
                 {   
-                    x = 1
+                    pos.x = 1
                 }
-                if (y < 2)
+                if (pos.y < 2)
                 {
-                    y = ny-1
+                    pos.y = ny-1
                 }
-                else if (y > ny-1)
+                else if (pos.y > ny-1)
                 {
-                    y = 2
+                    pos.y = 2
                 }
             }
             lastPos = UInt(arc4random_uniform(6))
         }
 
-        drawCube(xi: x, yi: y, color: NSColor.greenColor(), skip: skip)
+        drawCube(color: NSColor.greenColor(), skip: skip)
     }
     
-    func drawCube(#xi: Int, yi: Int, color: NSColor, skip: Int)
+    func drawCube(#color: NSColor, skip: Int)
     {
-        let h = sizey
-        let w = sizex
+        let (w, h) = cubeSize()
         let s = h/2
-        
-        let x = xi * w
-        var y = yi * h
-        
-        if (xi%2 == 1)
-        {
-            y -= h/2
-        }
-        
+        let x = pos.x*w
+        let y = (pos.x%2 == 0) ? (pos.y*h) : (pos.y*h - h/2)
+     
         if skip != 0 { // top
             color.set()
             let path = NSBezierPath()


class KrkklView : ScreenSaverView {
    
    var sizex = 0
    var sizey = 0
    var x = 0
    var y = 0
    var lastPos:UInt = 0
    var nextPos:UInt = 0
    
    override init(frame: NSRect, isPreview: Bool) {
        super.init(frame: frame, isPreview: isPreview)

    }

    override func startAnimation() {
        super.startAnimation()
        setup()
        setAnimationTimeInterval(1.0 / 30.0)
        needsDisplay = true
    }
