big refactor for codegen