Implementing internal state machine