refactored function from changeState to toggleState