Fix distance computation bugs.