Switch to generics and remove casting