Various fixes and some cleanup on the state saving