Refactor mode keys to be a state machine.