refactored simulation.R