implementing full state machine