Improvements to state handling