improved header logic