     }
     
     open func stopSession() {
-        self.previewLayer.connection.isEnabled = false
+        if let previewLayer = self.previewLayer, let connection = previewLayer.connection {
+            connection.isEnabled = false
+        }
     }
     
     // MARK: - Callbacks

        
        if self.onFaceDetection != nil {
            let metadataOutput = AVCaptureMetadataOutput()
            metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue(label: "MetadataOutputQueue"))
            
            if self.captureSession.canAddOutput(metadataOutput) {
                self.captureSession.addOutput(metadataOutput)
                metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeFace]
            }
        }
        

            return
        }
        
        if let stillImageOutput = self.stillImageOutput, !stillImageOutput.isCapturingStillImage {
            self.captureButton.isEnabled = false
            
            DispatchQueue.global().async(execute: {
                if let connection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {
                    
                    connection.videoOrientation = self.currentOrientation.toAVCaptureVideoOrientation()
                    connection.videoScaleAndCropFactor = self.zoomScale
                    
