Fixed loop issue where template type could be null.