Make output more robust by ensuring that it gets written on failure