use a state machine to make things clearer