add list_headers and write only at end of while loop