improve code for emulator.c