Some refactoring for codegen