Refactoring and more work on metadata handling.