second version of list templating. uses heap allocation