bug fixes and more unit tests