diff --git a/main.go b/main.go
index ac7abcf1e351dbb607cabad7c9d6e1bcbb4ba8f9..60625a0ac0d217816290c6decb11236960139342 100644
--- a/main.go
+++ b/main.go
@@ -126,22 +126,22 @@ func errorHandler(errorFilesPath string) func(http.ResponseWriter, *http.Request
 		if err != nil {
 			code = 404
 			log.Printf("unexpected error reading return code: %v. Using %v", err, code)
+			err = nil
 		}
 		w.WriteHeader(code)
 
 		var file string
 		for _, ext := range exts {
 			filePath := fmt.Sprintf("%v/%v%v", errorFilesPath, code, ext)
-			if _, err := os.Stat(file); os.IsExist(err) {
-				file = filePath
-				break
-			}
-			scode := strconv.Itoa(code)
-			filePath = fmt.Sprintf("%v/%cxx%v", errorFilesPath, scode[0], ext)
-			if _, err := os.Stat(file); os.IsExist(err) {
-				file = filePath
-				break
+			if _, err := os.Stat(file); os.IsNotExist(err) {
+				scode := strconv.Itoa(code)
+				filePath = fmt.Sprintf("%v/%cxx%v", errorFilesPath, scode[0], ext)
+				if _, err := os.Stat(file); os.IsExist(err) {
+					continue
+				}
 			}
+			file = filePath
+			break
 		}
 		if file == "" {
 			log.Printf("unexpected error opening file: %v", err)