Refactored, tests added