Implementing finite state machines