working out bugs in simulation algorithm