Refactored header logic