Is there something against switching the KDTree library? Currently you are using a pretty old C implementation. It would for example be possible to use the scikit-learn [KDTree](http://scikit-learn.org/stable/modules/generate/sklearn.neighbors.KDTree.html).