Added state machine to process user input