kdtree very hard to write