Big refactor for new caching mechanism.