finite state machine version
vserion 3