Rewrite simulation code