finished function to save docs and metadata