Refactoring for readability. Added a couple of unit tests