refactored to remove duplication