Fix 'headers already sent' error