Refactoring, extract some methods to utility classes and add unit tests