more tests and some code reorganization