moved calculation releated tasks into calcDistance function