Primitive finite state machine implementation