Initial working version.

Includes tests.