Rewrite of simulation

Logic complete.
Still need to optimize code to do thing much better.