Speed up code and fixing the ordering of running the simulation