added tests; refactored duplicated code