Use some metaprogramming to reduce duplication.