Fix bug to writw errors to file