Cache refactoring, bug fixing, test