Restructuring and generalizing the text-templating code