                 return
             }
 
-            let cImage = image.ciImage ?? CIImage(cgImage: image.cgImage!)
+            guard let ciImage = CIImage(image: image) else {
+                return
+            }
 
             let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyLow])
-            let features = detector!.features(in: cImage)
-
-            if features.count > 0 {
+            let features = detector!.features(in: ciImage)
+
+            if features.isEmpty == false {
                 if self.debugFaceAware == true {
                     print("found \(features.count) faces")
                 }
-                let imgSize = CGSize(width: Double(image.cgImage!.width), height: (Double(image.cgImage!.height)))
+                let imgSize = CGSize(width: image.cgImage!.width, height: 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 center = CGPoint(x: rect.origin.x + rect.size.width / 2.0, y: rect.origin.y + rect.size.height / 2.0)
        var offset = CGPoint.zero
        var finalSize = size

        DispatchQueue.main.async {
            if size.width / size.height > self.bounds.size.width / self.bounds.size.height {
                finalSize.height = self.bounds.size.height
                finalSize.width = size.width/size.height * finalSize.height
                center.x = finalSize.width / size.width * center.x
                center.y = finalSize.width / size.width * center.y

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

    public var focusOnFaces: Bool {
        set {
            let image = self.image
            set(image: image, focusOnFaces: newValue)
        } get {
            return sublayer() != nil ? true : false
