Fix bugs in simulation