Use the state instead of the state object directly.

If a state gets reset and you still hold a pointer to the previous,
incorrect, state object you'll operate on the wrong object. Using the
state to set/get objects and attributes you won't have this problem
since the state will always have the correct object.
Switch EXT* codes
Moved state reset to defered function
Updated tests