Tidying up saving the metadata

@lnlina plz review and merge