more robust header extraction