fixed a small bug to calculate last position