Refactor state machine usage