finally finished debugging the simulation function, it seems to work now.