Ex 5.2: Fixed errors in distance function.