Initial refactoring of code generation section

Refactored most of the code generation section, although there is still some work to be done around cgList / cgArgs.