refactoring and add test code.