optimized code: replaced often string concatenation with table.concat