         
         // Presents a drawable object when the command buffer is executed.
         commandBuffer!.presentDrawable(drawable)
+        
+        commandBuffer!.addCompletedHandler { (buffer:MTLCommandBuffer) -> Void in
+            dispatch_semaphore_signal(self.inflightSemaphore)
+        }
+        
         // The CommandBuffer is executed by the GPU.
         commandBuffer!.commit()
     }

    
    init(device: MTLDevice) {
        self.device = device
        makePipeline()
        makeBuffers()
    }


    
    func drawInView(view: MBEMetalView) {
        dispatch_semaphore_wait(inflightSemaphore, DISPATCH_TIME_FOREVER)
        
        updateUniforms(view, duration: view.frameDuration)
        
        // MTLTextures are containers for images, where each image is called a slice.
