Bugfixes and added UnitTest.