Better typeclass implementation.

Needs testing!