Use function template to reduce code duplication