Factorise simulation code.