Refactoring cgen a bit