Refactored to eliminate duplication.