Simplify/Improve template function

Small improvement on template function / Simplify code