some improvements and fixes, still a buggy nearest neighbour lookup