optimizes finite state machine, adds more handlers