refactor fsm. cleaup code.