version 2; refactored; all tests pass