Rework simulation code and add analysis code