Refactored code to better use generics.