Refacoring and support for recursive templates