Refactoring codegen