Remove profiling code
Reduce the number of calls to the distance function