Tweaked caching algorithm.