Fix error where the headers were getting written multiple times