Refactor code generation