Refactoring and tests added