We should move this logic into the btree/kdtree objects (not this PR, but in long run).