Added tests and bugfixes.