Minor fix on calculate distance function