         if(self.captureSession != nil){
         backgroundThread(delay: 0, background: {
             self.captureSession!.stopRunning()
-            self.captureVideoPreviewLayer!.removeFromSuperlayer()
+            self.cameraView.removePreviewLayer()
             self.captureVideoPreviewLayer = nil
             self.metaOutput = nil
             self.captureSession = nil

                metaOutput!.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
                metaOutput!.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
                captureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                captureVideoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
                captureVideoPreviewLayer!.frame = cameraView.bounds
                cameraView.layer.addSublayer(captureVideoPreviewLayer!)
                captureSession!.startRunning()
            }
            return true

    override func pluginInitialize() {
        super.pluginInitialize()
        NotificationCenter.default.addObserver(self, selector: #selector(pageDidLoad), name: NSNotification.Name.CDVPageDidLoad, object: nil)
        self.cameraView = CameraView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        self.cameraView.autoresizingMask = [.flexibleWidth, .flexibleHeight];
    }

    func sendErrorCode(command: CDVInvokedUrlCommand, error: QRScannerError){
