Restructuring the metadata fetching code