Refactored to remove repetitive code