some code reorganization and modified tests