improved caching to save to temporary directory