Could probably further reduce the duplication here, although the generics might get hairy.