Refactored state machine, added event queue