This PR includes more lines of code in state machine and test refactoring