Adding caching to the most expensive part of the code found so far.