Various tidyups and bugfixes, (hopefully) fixes #17