fix some bugs in document saving.