Refactoring and add test