Abstract clustering algorithm from KPTreeController (ugly, but works)