Generic deriving

Partially reverts 5ad45eff.