Almost working code of code generation