         do{
             try device.lockForConfiguration();
             if(backCameraDevice!.hasFlash){
-                if(backCameraDevice!.isFlashModeSupported(AVCaptureDevice.FlashMode.auto)){
-                    device.flashMode = AVCaptureDevice.FlashMode.auto;
+                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(AVCaptureDevice.TorchMode.off)){
-                    device.torchMode = AVCaptureDevice.TorchMode.off;
+                if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.off)){
+                    device.torchMode = AVCaptureTorchMode.off;
                 }
             }else{
                 let image = UIImage(named: "camera_flash_off.png") as UIImage!

                try device.lockForConfiguration();

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

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

                    flashState = 0;


        HappieCameraJSON.initializeProcessingCount();

        _ = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)

        let devices = AVCaptureDevice.devices()

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