use reduce instead of recursion, make templates vars instead of fns