Better error handling and log file truncation on success