Code refactoring and unit tests