little bit of refactoring for clarity, better comments & var names, added a test for use in a one-liner