refactoring test code and adding tests.