Much needed refactoring and clean up. Made it easier to write custom exporters.