Fix for distance calculation