Refactored and tests added