Seems to be a reasonable implementation.

Needs tests and documentation, though.