Add initial unit test, plus a couple of fixes.