Rework caching to remove memory leak.