Merge "refactor save_state code"