Fixed state saving, fixed some bugs