Refactored SpatialIndex to fix a memory leak.