cleaned up code using generics, etc.