refactored to eliminate code duplication