 
         HappieCameraJSON.initializeProcessingCount();
 
-        _ = AVCaptureDevice.devices(for: AVMediaType.video)
+        _ = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
 
         let devices = AVCaptureDevice.devices()
 
         // Loop through all the capture devices on this phone
-        for device in devices {
+        for device in devices! {
             // Make sure this particular device supports video
-            if ((device as AnyObject).hasMediaType(AVMediaType.video)) {
+            if ((device as AnyObject).hasMediaType(AVMediaTypeVideo)) {
                 // Finally check the position and confirm we've got the back camera
-                if((device as AnyObject).position == AVCaptureDevice.Position.back) {
+                if((device as AnyObject).position == AVCaptureDevicePosition.back) {
                     backCameraDevice = device as? AVCaptureDevice
                 }
             }

        if(canTakePhoto){
            HappieCameraJSON.incrementProcessingCount();
            canTakePhoto = false;
            let connection = stillImageOutput?.connection(with: AVMediaType.video)

            stillImageOutput?.captureStillImageAsynchronously(from: connection!) { imageBuffer, error in
                if((imageBuffer) != nil){
                    let imageData: Data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageBuffer!)!
                    let UIImageFromData = UIImage(data: imageData)

                    var  orient = UIImageOrientation.right;

                try device.lockForConfiguration();

                if(flashState == 0){
                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.off)){
                        device.flashMode = AVCaptureFlashMode.off;
                        let image = UIImage(named: "camera_flash_off.png") as UIImage!
                        flashUIButton.setImage(image, for: UIControlState())
                    }
                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.off)){
                        device.torchMode = AVCaptureTorchMode.off;
                    }
                    flashState = 1;
                }

                else if(flashState == 1){
                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.auto)){
                        device.flashMode = AVCaptureFlashMode.auto;
                        let image = UIImage(named: "camera_flash_auto.png") as UIImage!
                        flashUIButton.setImage(image, for: UIControlState())
                    }
                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.off)){
                        device.torchMode = AVCaptureTorchMode.off;
                    }
                    flashState = 2
                }
                else if(flashState == 2){
                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.off)){
                        device.flashMode = AVCaptureFlashMode.off;
                        let image = UIImage(named: "camera_flash_on.png") as UIImage!
                        flashUIButton.setImage(image, for: UIControlState())
                    }
                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.on)){
                        device.torchMode = AVCaptureTorchMode.on;
                    }

                    flashState = 0;
