Refactored test code to eliminate some duplication