             let leaf1: Bool = BVH.isLeaf(index1, elementCount: elementCount1)
             let leaf2: Bool = BVH.isLeaf(index2, elementCount: elementCount2)
             if leaf1, leaf2 {
-                let elementIndex1 = self.leafNodeIndexToElementIndex(index1)
-                let elementIndex2 = other.leafNodeIndexToElementIndex(index2)
+                let elementIndex1 = BVH.leafNodeIndexToElementIndex(index1, elementCount: elementCount1, lastRowIndex: lastRowIndex1)
+                let elementIndex2 = BVH.leafNodeIndexToElementIndex(index2, elementCount: elementCount2, lastRowIndex: lastRowIndex2)
                 callback(elementIndex1, elementIndex2)
             }
             else if leaf1 {

        let elementCount2 = other.elementCount
        let boxes1 = self.boundingBoxes
        let boxes2 = other.boundingBoxes
        func intersects(index: Int, callback: (Int, Int) -> Void) {
            if BVH.isLeaf(index, elementCount: elementCount1) { // if it's a leaf node
                let elementIndex = self.leafNodeIndexToElementIndex(index)
                callback(elementIndex, elementIndex)
            }
            else {

            elementIndex += elementCount
        }
        return elementIndex
    }
    
    private static func isLeaf(_ index: Int, elementCount: Int) -> Bool {
