Refactoring code. (part 2). Code generation works (unstable) now.