refactor simulator into several smaller functions