Implementation of finite state machine