several fixes and unit tests