factored out most of the corpus save logic