Less stupid, better commented way to handle the state.