Bug fix: open error file only after creating dir