IMPROVEMENT: freed up a lot of memory use in the loop to calculate future positions - calulating minimum distance directly without storing interim results.