Refactor header handling