Refactor set_metadata