refactor emulatedEvent code