some refactoring and improvements with metadata management.