a lot of bug fixes, unit test fixes and improvements