refactured run_simulation and added error codes