externalized creation of csv and fixed error when key is not available in first line