Complete code for Simulation Caching, not tested.