Implementing calculation for distance