Fixed some bugs with state persistence