Fixed some bugs in caching