 		}
 	}
 	
-	var pageWidth: CGFloat {
-		switch scrollDirection {
-		case .horizontal:
-			return itemSize.width + minimumLineSpacing
-		case .vertical:
-			return itemSize.height + minimumLineSpacing
-		}
-	}
-	
 	/// Programatically scrolls to a page at a specified index.
 	///
 	/// - Parameters:

	public func scrollToPage(index: Int, animated: Bool) {
		guard let collectionView = collectionView else { return }
		
		let pageOffset: CGFloat
		let proposedContentOffset: CGPoint
		let shouldAnimate: Bool
		switch scrollDirection {
		case .horizontal:
			pageOffset = CGFloat(index) * pageWidth - collectionView.contentInset.left
			proposedContentOffset = CGPoint(x: pageOffset, y: 0)
			shouldAnimate = fabs(collectionView.contentOffset.x - pageOffset) > 1 ? animated : false
		case .vertical:
			pageOffset = CGFloat(index) * pageWidth - collectionView.contentInset.top
			proposedContentOffset = CGPoint(x: 0, y: pageOffset)
			shouldAnimate = fabs(collectionView.contentOffset.y - pageOffset) > 1 ? animated : false
		}
		collectionView.setContentOffset(proposedContentOffset, animated: shouldAnimate)
	}
	
	/// Calculates the current centered page.
	public var currentCenteredPage: Int? {
		guard let collectionView = collectionView else { return nil }
		let currentCenteredPoint = CGPoint(x: collectionView.contentOffset.x + collectionView.bounds.width/2, y: collectionView.contentOffset.y + collectionView.bounds.height/2)
		let indexPath = collectionView.indexPathForItem(at: currentCenteredPoint)
		return indexPath?.row
	}
}

	
	private var lastCollectionViewSize: CGSize = CGSize.zero
	private var lastScrollDirection: UICollectionViewScrollDirection!
    var pageWidth: CGFloat {
        switch scrollDirection {
        case .horizontal:
            return itemSize.width + minimumLineSpacing
        case .vertical:
            return itemSize.height + minimumLineSpacing
        }
    }
    
    /// Calculates the current centered page.
    public var currentCenteredPage: Int? {
        guard let collectionView = collectionView else { return nil }
        let currentCenteredPoint = CGPoint(x: collectionView.contentOffset.x + collectionView.bounds.width/2, y: collectionView.contentOffset.y + collectionView.bounds.height/2)
        
        return collectionView.indexPathForItem(at: currentCenteredPoint)?.row
    }
	
	public override init() {
		super.init()
