Added tests and bugfixes/improvements.