     
         if fromVC != toVC, let fromVC = fromVC, let toVC = toVC {
             // Replace existing view controller with new view controller
+            addChildViewController(toVC)
             toVC.willMoveToParentViewController(self)
             fromVC.willMoveToParentViewController(nil)
             fromVC.view.removeFromSuperview()
             fromVC.removeFromParentViewController()
             fromVC.didMoveToParentViewController(nil)
-            addChildViewController(toVC)
             setupContainer(viewContainer, withViewController: toVC)
             toVC.didMoveToParentViewController(self)
             return true
                 
         } else if fromVC == nil, let toVC = toVC {
                 // add new view controller
+                addChildViewController(toVC)
                 toVC.willMoveToParentViewController(self)
-                addChildViewController(toVC)
                 setupContainer(viewContainer, withViewController: toVC)
                 toVC.didMoveToParentViewController(self)
                 return true

        
        if fromVC != toVC && isAnimated, let fromVC = fromVC, let toVC = toVC {
            // Animated transition between view controllers
            toVC.willMoveToParentViewController(self)
            fromVC.willMoveToParentViewController(nil)
            transitionFromViewController(fromVC, toViewController: toVC, duration: animationDuration, options: animationOptions, animations: nil, completion: { isFinished in
                fromVC.view.removeFromSuperview()
                fromVC.removeFromParentViewController()
                fromVC.didMoveToParentViewController(nil)
                self.addChildViewController(toVC)
                self.setupContainer(viewContainer, withViewController: toVC)
                toVC.didMoveToParentViewController(self)
                completion(isFinished)

    
    func replaceFromViewController(fromVC: UIViewController?, toViewController toVC: UIViewController?, inContainer viewContainer: UIView, duration animationDuration: NSTimeInterval, options animationOptions: UIViewAnimationOptions, completion completionBlock: ((Bool) -> Void)?) {
        
        let isAnimated = animationDuration > 0 && self.childViewControllers.count > 0
        let completion: (Bool) -> (Void) = { isFinished in
            if let completionBlock = completionBlock {
                completionBlock(isFinished)
