Fixed errors in distance calculation code.