kdtree based on Originally, Rednaxela. Improved by Stephen Roller and Ben Wing.