First version.

Still a couple of bugs to sort out.