         }
     }
 
-    private func applyFaceDetection(for features: [AnyObject], size: CGSize, image: UIImage) {
-        var rect = features[0].bounds!
+    private func applyFaceDetection(for features: [CIFeature], size: CGSize, image: UIImage) {
+        var rect = features[0].bounds
         rect.origin.y = size.height - rect.origin.y - rect.size.height
         var rightBorder = Double(rect.origin.x + rect.size.width)
         var bottomBorder = Double(rect.origin.y + rect.size.height)
 
         for feature in features[1..<features.count] {
-            var oneRect = feature.bounds!
+            var oneRect = feature.bounds
             oneRect.origin.y = size.height - oneRect.origin.y - oneRect.size.height
             rect.origin.x = min(oneRect.origin.x, rect.origin.x)
             rect.origin.y = min(oneRect.origin.y, rect.origin.y)

                return
            }

            let cImage = image.ciImage ?? CIImage(cgImage: image.cgImage!)

            let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyLow])
            let features = detector!.features(in: cImage)

            if features.count > 0 {
                if self.debugFaceAware == true {
                    print("found \(features.count) faces")
                }
                let imgSize = CGSize(width: Double(image.cgImage!.width), height: (Double(image.cgImage!.height)))
                self.applyFaceDetection(for: features, size: imgSize, image: image)
            } else {
                if self.debugFaceAware == true {

        rect.size.width = CGFloat(rightBorder) - rect.origin.x
        rect.size.height = CGFloat(bottomBorder) - rect.origin.y

        var offset = CGPoint.zero
        var finalSize = size

        DispatchQueue.main.async {
            if size.width / size.height > self.bounds.size.width / self.bounds.size.height {
                var centerX = rect.origin.x + rect.size.width / 2.0

                finalSize.height = self.bounds.size.height
                finalSize.width = size.width/size.height * finalSize.height
                centerX = finalSize.width / size.width * centerX

                offset.x = centerX - self.bounds.size.width * 0.5
                if offset.x < 0 {
                    offset.x = 0
                } else if offset.x + self.bounds.size.width > finalSize.width {
