     
     @available(iOS 9.0, *)
     private func cancelPeekFor(peekAndPopData: PeekAndPopData) {
-        peekState = .finished
+        resetPeekState()
         
         guard let viewController = peekAndPopData.peekViewController else {
             return

        
        switch peekState {
        case .waitingForPeekAndPopData:
            let fullPopAction: (() -> ())
            
            if let navigationController = viewController.navigationController {
                // (*) If you present a `viewController` in a `peek` mode, 
                // whereas the `viewController` is already embeded into a `parent` controller 
                // (i.e.: `UINavigationController` and/or probably `UIPopoverController`),
                // then `UIKit` will require you to unbind the `viewController` from its `parent`
                let filteredViewControllers = navigationController.viewControllers.filter { $0 !== viewController }
                navigationController.viewControllers = filteredViewControllers
                
                fullPopAction = {
                    // Return `viewController` back to its `parent`
                    let restoredViewControllers = navigationController.viewControllers + [viewController]
                    navigationController.viewControllers = restoredViewControllers                        
                    
                    popAction()
                }
            } else {
                fullPopAction = popAction
            }

            if viewController.parent != nil {
                // Probably an unhandled edge case. See (*) for details
                debugPrint(
                    "The following code may crash your app with `NSInvalidArgumentException` \n"
                        + "reason: 'Application tried to present modally an active controller ... \n"
                        + "If so, please report an issue at a github repo page"
                )
            }
            
            let peekAndPopData = PeekAndPopData(
                peekViewController: viewController,
                popAction: fullPopAction
            )
            
            peekState = .receivedPeekAndPopData(peekAndPopData)

            
            // Notify about a reregister
            registeredPreviewingData.onPreviewingContextChange?(newPreviewingContext)
        }
    }
    
    private func resetPeekState() {
        peekState = .finished
        peekGestureRecognizer = nil
    }
    
    private func unbindViewControllerFromParent(
        viewController: UIViewController,
        rollback: inout (() -> ())?)
    {
        if let navigationController = viewController.navigationController {
            // (*) If you present a `viewController` in a `peek` mode, 
            // whereas the `viewController` is already embeded into a `parent` controller 
            // (i.e.: `UINavigationController` and/or probably `UIPopoverController`),
            // then `UIKit` will require you to unbind the `viewController` from its `parent`
            let filteredViewControllers = navigationController.viewControllers.filter { $0 !== viewController }
            navigationController.viewControllers = filteredViewControllers
            
            rollback = {
                // Return `viewController` back to its `parent`
                let restoredViewControllers = navigationController.viewControllers + [viewController]
                navigationController.viewControllers = restoredViewControllers                        
            }
        }
        
        if viewController.parent != nil {
            // Probably an unhandled edge case. See (*) for details
            debugPrint(
                "The following `peek` may crash your app with `NSInvalidArgumentException` \n"
                    + "reason: 'Application tried to present modally an active controller ...'. \n"
                    + "If so, please report an issue at a github repo page"
            )
        }
    }
    
    @objc private func onPeekGestureChange(_ sender: UIGestureRecognizer) {
        // When a user cancels `peek`, gesture recognizer's state is `.ended`
        // When a user commits `peek`, gesture recognizer's state is `.cancelled`
        if sender.state == .ended {
            // Release the `peek` view controller
            resetPeekState()
        }
    }
}
