Refactored to DRY up code