 
   private var colorSpace: CGColorSpaceRef?
 
-  private static let φ: CGFloat = (1 + sqrt(5.0)) / 2.0
+  private let device: MTLDevice! = MTLCreateSystemDefaultDevice()
+
+  lazy var commandQueue: MTLCommandQueue = {
+    return self.device.newCommandQueue()
+  }()
+
+  lazy var commandBuffer: MTLCommandBuffer = {
+    return self.commandQueue.commandBuffer()
+  }()
 
   private func tileImage(image: CIImage) -> CIImage? {
-    guard let device = MTLCreateSystemDefaultDevice() else {
+    guard let colorSpace = colorSpace else {
       return nil
     }
 

      return nil
    }

    let canvas = CGRect(x: 0, y: 0, width: floor(dimension * self.dynamicType.φ), height: dimension)
    let descriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: Int(canvas.width), height: Int(canvas.height), mipmapped: false)
    descriptor.usage = [.RenderTarget, .ShaderRead, .ShaderWrite]

    let texture = device.newTextureWithDescriptor(descriptor)
    let commandQueue = device.newCommandQueue()
    let commandBuffer = commandQueue.commandBufferWithUnretainedReferences()

    guard let colorSpace = colorSpace else {
      return nil
    }

    context.render(cropped, toMTLTexture: texture, commandBuffer: commandBuffer, bounds: canvas, colorSpace: colorSpace)

    commandBuffer.commit()

    return CIImage(MTLTexture: texture, options: [kCIImageColorSpace: colorSpace])
  }

  private func squareCropImage(image: CIImage, toDimension dimension: CGFloat) -> CIImage? {
    let scale = dimension / min(image.extent.width, image.extent.height)
    let filter = CIFilter(name: "CILanczosScaleTransform", withInputParameters: [
      kCIInputImageKey: image,
      kCIInputScaleKey: scale
      ])

    guard let rescaled = filter?.valueForKey(kCIOutputImageKey) as? CIImage else {
      return nil
    }

    let context = CIContext()
    let detector = CIDetector(ofType: CIDetectorTypeFace, context: context, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
    let orientation: AnyObject = image.properties[kCGImagePropertyOrientation as String] ?? 1
    let features = detector.featuresInImage(rescaled, options: [CIDetectorImageOrientation: orientation])

    let cropRect: CGRect
    if features.count == 0 {
      cropRect = CGRect(x: (rescaled.extent.width - dimension) / 2.0, y: (rescaled.extent.height - dimension) / 2.0, width: dimension, height: dimension)
    }
    else {
      // We've detected some faces, set crop rect to center around faces

      cropRect = facesRect
    }
    
    let cropped = image.imageByCroppingToRect(cropRect)
    let transform = CGAffineTransformMakeTranslation(-cropped.extent.minX, -cropped.extent.minY)
    return cropped.imageByApplyingTransform(transform)
  }

  private func createTexture(width width: Int, height: Int) -> MTLTexture {
    let descriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.BGRA8Unorm, width: width, height: height, mipmapped: false)
    descriptor.usage = [.ShaderRead, .ShaderWrite]
    return device.newTextureWithDescriptor(descriptor)
  }

  private func createScaledTexture(sourceTexture: MTLTexture, dimension: Int) -> MTLTexture {
    let scale = MPSImageLanczosScale(device: device)
    let factor = Double(dimension) / Double(sourceTexture.width)
    var scaleTransform = MPSScaleTransform(scaleX: factor, scaleY: factor, translateX: 0, translateY: 0)

    withUnsafePointer(&scaleTransform) { ptr in
      scale.scaleTransform = ptr
    }

    let texture = createTexture(width: dimension, height: dimension)
    scale.encodeToCommandBuffer(commandBuffer, sourceTexture: sourceTexture, destinationTexture: texture)
    return texture
  }

  private func transposeTexture(sourceTexture: MTLTexture) -> MTLTexture {
    let texture = createTexture(width: sourceTexture.height, height: sourceTexture.width)

    let transpose = MPSImageTranspose(device: device)
    transpose.encodeToCommandBuffer(commandBuffer, sourceTexture: sourceTexture, destinationTexture: texture)

    return texture
  }

}
