more efficient templated code