More unit tests, some code refinements