         }
         func intersects(index: Int, callback: (Int, Int) -> Void) {
             if index >= inodecount { // if it's a leaf node
-                callback(index - inodecount, index - inodecount)
+                let elementIndex1 = BVH.leafNodeIndexToElementIndex(index, leafCount: self.elementCount, lastRowIndex: lastRowIndex)
+                callback(elementIndex1, elementIndex1)
             }
             else {
                 let l = 2*index+1

        }
    }
    
    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])

            return .internal
        }
        else {
            return .leaf(elementIndex: BVH.leafNodeIndexToElementIndex(index, leafCount: self.bvh.takeUnretainedValue().elementCount, lastRowIndex: self.bvh.takeUnretainedValue().lastRowIndex ))
        }
    }
    
