added try/except block for CSV writing