refactoring the check if the document exists