Optimization of matching using kdtree.