added next state logic