Major refactor into typeclass with instances.