Initial code

This is the initial code. It probably has bugs, but it appears to be
working.