subroutine added to calculate ditance between currend and last position