Further fixes and improvements to state management