Cleaned up and commented some of the code in the emulation script.