         
         let currentOrientation = AVCaptureVideoOrientation.avorientationFromUIDeviceOrientation(UIDevice.current.orientation)
     
-        if self.previewLayer.connection.isVideoOrientationSupported {
-            self.previewLayer.connection.videoOrientation = currentOrientation
-        }
-
+        if let previewConnection = self.previewLayer.connection {
+            if previewConnection.isVideoOrientationSupported {
+                previewConnection.videoOrientation = currentOrientation
+            }
+        }
+            
         if let videoOutput = self.videoOutput, let videoConnection = videoOutput.connection(withMediaType: AVMediaTypeVideo) {
             if videoConnection.isVideoOrientationSupported {
                 videoConnection.videoOrientation = currentOrientation

    
    public var mirroringMode: NextLevelMirroringMode {
        get {
            let previewConnection = self.previewLayer.connection
            if let pc = previewConnection {
                if pc.isVideoMirroringSupported {
                    if !pc.automaticallyAdjustsVideoMirroring {
                        return pc.isVideoMirrored ? .on : .off

        }
        set {
            if let _ = self.captureSession, let videoOutput = self.videoOutput {

                switch newValue {
                case .off:
                    if let vc = videoOutput.connection(withMediaType: AVMediaTypeVideo) {
                        if vc.isVideoMirroringSupported {
                            vc.isVideoMirrored = false
                        }
                    }
                    if let pc = self.previewLayer.connection {
                        if pc.isVideoMirroringSupported {
                            pc.isVideoMirrored = false
                            pc.automaticallyAdjustsVideoMirroring = false
