many more fixes and unit tests