added check for documents and made sort not required