Lots of bugfixes to the merge.
Issue #57 is probably 80% done.