Refactorings and added test coverage