added logic to state changes