Metadata refactoring: complete MetadataRepository rewrite (to be continued).