Fixed most of state machine erors