Refactored some caching code