Added tests and minor bugfixes.