Reduce number of global variables used

This is the first step to make the exporter more testable.

@u-c-l @brian-brazil @discordianfish