Fix bug in distance