Refactor machine not to use goto