cleaned up state machine