minor refactoring and tests added