Refactor document save