Refactor _format_metadata