MISC: refactoring, improve caching logic. Still work in progress