Refactored duplicate code