Initial code commit.

Needs more unit tests, but it should work.