Added logic for document loading