Add tryCatch to write functions (see issue #7)