             isInBounds = (UInt(position.y) < self.maximumNumberOfCellsInBounds);
             hasMaximumBoundSize = (cellOrigin.y == 0);
           }
-
+          
           if hasSpaceAvailable && hasMaximumBoundSize && !isInBounds {
             print("View is not large enough to hold cell... inserting anyway.")
             return true
           }
-
+          
           return (hasSpaceAvailable && isInBounds)
         }
       )
-
+      
       if !didTraverseAllCells {
         return true
       }
-
+      
       self.setIndexPath(indexPath, forPosition: cellOrigin)
-
+      
       self.traverseCellsForPosition(
         cellOrigin,
         withSize: aCellSize,
         closure: {[unowned self] (let aPosition: CGPoint) in
           self.setPosition(aPosition, forIndexPath: indexPath)
           self.furthestCellPosition = aPosition
-
+          
           return true
         }
       )
-
+      
       return false
       }
     )
   }
-
+  
   private func insertCellsToIndexPath(indexPath: NSIndexPath) {
     for section in indexPathCache.section..<collectionView!.numberOfSections() {
       for row in indexPathCache.row + 1..<collectionView!.numberOfItemsInSection(section) {
-
+        
         // Return if we are past the desired row
         if section >= indexPath.section && row > indexPath.row {
           return
         }
-
+        
         let newIndexPath = NSIndexPath(forItem: row, inSection: section)
-
+        
         if insertCellAtIndexPath(indexPath) {
           indexPathCache = newIndexPath
         }
       }
     }
   }
-
+  
   private func insertCellsToUnboundIndex(unboundIndex: Int) {
     let sectionCount = collectionView?.numberOfSections() ?? 0
-
+    
     //for var aRow = row + 1; row < rows; ++aRow
     for section in indexPathCache.section..<sectionCount {
       let rowCount = collectionView?.numberOfItemsInSection(section) ?? 0
-
+      
       for row in indexPathCache.row + 1..<rowCount {
         let indexPath = NSIndexPath(forRow: row, inSection: section)
-
+        
         if insertCellAtIndexPath(indexPath) {
           indexPathCache = indexPath
         }
-
+        
         // Test whether we are beyond the unbound index.
         switch scrollDirection {
         case .Vertical:
-          if Int(firstOpenSpace.y) >= unboundIndex {
+          guard Int(firstOpenSpace.y) < unboundIndex else {
             return
           }
-
+          
           break
-
+          
         case .Horizontal:
-          if Int(firstOpenSpace.x) >= unboundIndex {
+          guard Int(firstOpenSpace.x) < unboundIndex else {
             return
           }
-
+          
           break
-
         }
       }
     }

      }
    }
  }

  // This is a map of integers to dictionaries of integers mapped to index paths
  var indexPathByPosition = [Int: [Int: NSIndexPath]]()
  var positionByIndexPath = [UInt: [UInt: NSValue]]()

  //-- Caching
  // The variables below are used for caching to prevent too much work.
  var layoutAttributesCache = [UICollectionViewLayoutAttributes]()
  var layoutRectCache = CGRectZero
  var indexPathCache = NSIndexPath()

  var maximumNumberOfCellsInBounds: UInt {

    guard let cv = collectionView else {
      // This will never happen, but collection view is an optional, so we have to unwrap it.
      return 0
    }

    var size: UInt = 0;
    let contentRect = UIEdgeInsetsInsetRect(cv.frame, cv.contentInset)

    switch scrollDirection {
    case .Vertical:
      size = UInt(CGRectGetWidth(contentRect) / cellSize.width)
    case .Horizontal:
      size = UInt(CGRectGetHeight(contentRect) / cellSize.height)
    }

    if size == 0 {
      print("Cannot fit cell in contect rect. Defaulting to 1")
      return 1
    }

    return size
  }
}

          let hasSpaceAvailable: Bool = Bool(self.indexPathForPosition(position) == nil)
          var isInBounds: Bool = false
          var hasMaximumBoundSize: Bool = false
          
          switch self.scrollDirection {
          case .Vertical:
            isInBounds = (UInt(position.x) < self.maximumNumberOfCellsInBounds);
