More refactor for code gen