Refactoring code to use generics