BY: some re-arrangements
Merge branch 'BY-calculatedistance'