Minor refactor of caching code