Use templates to reduce code duplication.