Attempt to fix finite state machine

for issue #10

Please check for improvements. Thanks.