test-first refactor, to eliminate duplicated code