             resumeReading()
         }
         
-        captureSession = AVCaptureSession()
-        
-        guard let captureSession = captureSession else {
+        self.captureSession = AVCaptureSession()
+        
+        guard let captureSession = self.captureSession else {
             return
         }
         

        let input = try AVCaptureDeviceInput(device: captureDevice)
        captureSession.addInput(input)
        
        captureSession.addOutput(captureMetadataOutput)
        captureMetadataOutput.metadataObjectTypes = codeTypes
        
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer?.videoGravity = .resizeAspectFill
        videoPreviewLayer?.frame = self.layer.bounds
        self.layer.addSublayer(videoPreviewLayer!)
        
        isScanning = true
        
        q.async {
            captureSession.startRunning()
        }
        captureMetadataOutput.rectOfInterest = videoPreviewLayer!.metadataOutputRectConverted(fromLayerRect: scanRect)
    }
    
    @objc public func stopReading() {

    public override func layoutSubviews() {
        super.layoutSubviews()
        videoPreviewLayer?.frame = self.bounds
        if isScanning || isFreezing, let videoPreviewLayer = videoPreviewLayer {
            captureMetadataOutput.rectOfInterest = videoPreviewLayer.metadataOutputRectConverted(fromLayerRect: scanRect)
            updatePreviewLayerOrientation(videoPreviewLayer)
        }
    }
    
    private func updatePreviewLayerOrientation(_ previewLayer: AVCaptureVideoPreviewLayer) {
        guard let previewLayerConnection = previewLayer.connection else {
            return
        }
        
        if previewLayerConnection.isVideoOrientationSupported {
            switch (UIDevice.current.orientation) {
            case .portrait: previewLayerConnection.videoOrientation = .portrait
            case .landscapeRight: previewLayerConnection.videoOrientation = .landscapeLeft
            case .landscapeLeft: previewLayerConnection.videoOrientation = .landscapeRight
            case .portraitUpsideDown: previewLayerConnection.videoOrientation = .portraitUpsideDown
            default: previewLayerConnection.videoOrientation = .portrait
                break
            }
        }
        
        previewLayer.frame = self.bounds
    }
    
    deinit {
