Minor metadata handling cleanup

This code would benefit from a more general library to make
metadata handling easier.