Cleanup and caching code