Improve caching
Most of #418
Minor change