Revert "Revert "big refactor for codegen""

This reverts commit 6bbc3da924f246672acd430ef9268335939b6151.
new base