added stricter error checking to writing to log file