Fix bug in distance calculation.