Metadata-related fixes and additions.

This includes adding a bunch of tests.