Factor out export code into a module

Fixes #57