Refactored to minimize code duplication