Initial implementation.

See commits for comments.