changed so that the button state updates first