Corrected average_error loop