rewrite the state logic