Completed function to generate final response