improved parsing of templates