Cleanup: break code generator to smaller functions.