             let leaf1 = index1 >= inodecount
             let leaf2 = index2 >= inodecount
             if leaf1, leaf2 {
-                if index1 < index2 {
-                    callback(index1 - inodecount, index2 - inodecount)
-                }
-                else {
-                    callback(index2 - inodecount, index1 - inodecount)
-                }
+                let elementIndex1 = BVH.leafNodeIndexToElementIndex(index1, leafCount: self.elementCount, lastRowIndex: lastRowIndex)
+                let elementIndex2 = BVH.leafNodeIndexToElementIndex(index2, leafCount: self.elementCount, lastRowIndex: lastRowIndex)
+                callback(elementIndex1, elementIndex2)
             }
             else if leaf1 {
                 intersects(index1: index1, index2: 2*index2+1, callback: callback)

        }
    }
    
    init(boxes leafBoxes: [BoundingBox]) {
        // create a complete binary tree of bounding boxes where boxes[0] is the root and left child is 2*index+1 and right child is 2*index+2
        let boxes = UnsafeMutablePointer<BoundingBox>.allocate(capacity: 2*leafBoxes.count-1)
        self.inodeCount = leafBoxes.count-1
        for i in 0..<leafBoxes.count {
            boxes[i+self.inodeCount] = leafBoxes[i]
        }
        for i in stride(from: self.inodeCount-1, through: 0, by: -1) {
            boxes[i] = BoundingBox(first: boxes[2*i+1], second: boxes[2*i+2])

            let leaf1 = index1 >= inodecount1
            let leaf2 = index2 >= inodecount2
            if leaf1, leaf2 {
                let elementIndex1 = BVH.leafNodeIndexToElementIndex(index1, leafCount: self.elementCount, lastRowIndex: self.lastRowIndex)
                let elementIndex2 = BVH.leafNodeIndexToElementIndex(index2, leafCount: other.elementCount, lastRowIndex: other.lastRowIndex)
                callback(elementIndex1, elementIndex2)
            }
            else if leaf1 {
                intersects(index1: index1, index2: 2*index2+1, callback: callback)
