Improve caching to be more robust