Added a simple template system and used it to clean up code generation.