split unrelated concerns out of calcdistances function