Finite State Machine implementation