refactored and simplified code generation