249 Refactoring and add tests