Implemented a finite state machine.