fixed unit tests and bugs revealed by unit tests