rewrite simulation loop