codegen cleanup and bugfixes