Added a condition to avoid the automatic reset 
if(previousState != state) {setState()} ...