More unit testing and bugfixes.