Initial version of code generation (still a bloody hack!)