fixed caching logic and some refactoring