cleaned code and fixed speed of emulation