Refactoring and add test case
refactoring and add test case.