Finished tests for DocumentRepository a lot of bug fixing and refactoring.