Refactor ModuleTransformer

This is to make it easier to subclass and override what code to generate.

See issue #461 for motivation.