         if let validCaptureSession = captureSession {
             previewLayer = AVCaptureVideoPreviewLayer(session: validCaptureSession)
             previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
+        }
+    }
+    
+    private func _updateCameraDevice(deviceType: CameraDevice) {
+        if let validCaptureSession = captureSession {
+            validCaptureSession.beginConfiguration()
+            let inputs = validCaptureSession.inputs as! [AVCaptureInput]
+            
+            for input in inputs {
+                if let deviceInput = input as? AVCaptureDeviceInput {
+                    if deviceInput.device == backCameraDevice && cameraDevice == .Front {
+                        validCaptureSession.removeInput(deviceInput)
+                        break;
+                    } else if deviceInput.device == frontCameraDevice && cameraDevice == .Back {
+                        validCaptureSession.removeInput(deviceInput)
+                        break;
+                    }
+                }
+            }
+            switch cameraDevice {
+            case .Front:
+                if hasFrontCamera {
+                    if let validFrontDevice = _deviceInputFromDevice(frontCameraDevice) {
+                        if !inputs.contains(validFrontDevice) {
+                            validCaptureSession.addInput(validFrontDevice)
+                        }
+                    }
+                }
+            case .Back:
+                if let validBackDevice = _deviceInputFromDevice(backCameraDevice) {
+                    if !inputs.contains(validBackDevice) {
+                        validCaptureSession.addInput(validBackDevice)
+                    }
+                }
+            }
+            validCaptureSession.commitConfiguration()
         }
     }
     

        }
    }
    
    private func _addVideoInput() {
        cameraDevice = _cameraDevice
    }
    
    private func _setupOutputMode(newCameraOutputMode: CameraOutputMode) {
        captureSession?.beginConfiguration()
        

            if let validCaptureSession = self.captureSession {
                validCaptureSession.beginConfiguration()
                validCaptureSession.sessionPreset = AVCaptureSessionPresetHigh
                self._updateCameraDevice(.Back)
                self._setupOutputs()
                self._setupOutputMode(self._cameraOutputMode)
                self.cameraOutputQuality = self._cameraOutputQuality
