fixed a bug that could cause the state to flip flop