Improve code generation for user defined functions