working simple state machine