Added caching for calculation distances.