refactored some code and added many comments to simulations.js