fixed bug with distance calculation