Improve caching logic