Factor out some caching logic and such