Moving more logic to state machine