Factor out code generation to a separate module