Refactoring and Add UnitTest