fixed a distance function, workable code!