Fixed allocation of the kd-tree; fixed minor bugs in nearest neighbor search; seems to work well now