 extension SAHistoryNavigationViewController: SAHistoryViewControllerDelegate {
     func didSelectIndex(index: Int) {
         
-        let viewControllers = self.viewControllers
+        if let viewControllers = self.viewControllers as? [UIViewController] {
+            var destinationViewController: UIViewController?
+            for (currentIndex, viewController) in enumerate(viewControllers) {
+                if currentIndex == index {
+                    destinationViewController = viewController
+                    break
+                }
+            }
         
-        //var destinationViewController: UIViewController?
-        var newViewControllers = [AnyObject]()
-        for (currentIndex, viewController) in enumerate(viewControllers) {
-            newViewControllers += [viewController]
-            if currentIndex == index {
-                break
+            if let viewController = destinationViewController {
+                popToViewController(viewController, animated: false)
             }
-        }
-    
-        //self.popToViewController(nil, animated: false)
-    
-        screenshotImages.removeAll(keepCapacity: false)
-        setViewControllers(newViewControllers, animated: false)
-        
-        UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: {
-            self.historyViewController.view.transform = CGAffineTransformIdentity
-            self.historyViewController.scrollToIndex(index, animated: false)
-        }) { (finished) in
-            self.historyViewController.view.hidden = true
-            self.coverView.hidden = true
-            self.setNavigationBarHidden(false, animated: true)
+            
+            UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: {
+                self.historyViewController.view.transform = CGAffineTransformIdentity
+                self.historyViewController.scrollToIndex(index, animated: false)
+            }) { (finished) in
+                self.historyViewController.view.hidden = true
+                self.coverView.hidden = true
+                self.setNavigationBarHidden(false, animated: true)
+            }
         }
     }
 }

    
    public override func setViewControllers(viewControllers: [AnyObject]!, animated: Bool) {
        super.setViewControllers(viewControllers, animated: animated)
        if let viewControllers = viewControllers as? [UIViewController] {
            for viewController in viewControllers {
                screenshotImages += [viewController.view.screenshotImage(scale: kImageScale)]
            }
        }

    }
    
    public override func popToViewController(viewController: UIViewController, animated: Bool) -> [AnyObject]? {
        
        var index: Int?
        for (currentIndex, currentViewController) in enumerate(viewControllers) {
            if currentViewController as? UIViewController == viewController {
                index = currentIndex
                break
            }
        }
        
        var removeList = [Bool]()
        for (currentIndex, image) in enumerate(screenshotImages) {
            if currentIndex > index {
                removeList += [true]
            } else {
                removeList += [false]
            }
        }
        for (currentIndex, shouldRemove) in enumerate(removeList) {
            if shouldRemove {
                if let index = index {
                    screenshotImages.removeAtIndex(index + 1)
                }
            }
        }
        return super.popToViewController(viewController, animated: animated)
