factor out the duplication in the export handler