refactored to mostly using modify instead of set and put. This way theres no change of old state overwriting new state.