Simple implementation of finite state machine