refactor code gen