refactoring and tests added