Finite state machine improved.