refactored. Added tests