Low level caching and minor refactoring