rewrite caching code