Added check for headers already being sent in header function