This also looks like a recycle of the publish code, so let's refactor here, too.