Wells Fargo has reached a $386 million deal to settle a class-action lawsuit brought by customers who say the bank forced them to buy unnecessary auto insurance, putting to rest one of its many legal problems.

The agreement, described in court papers filed Thursday, would resolve a lawsuit filed in July 2017, shortly after The New York Times published the results of an internal report the bank had commissioned on the matter. The report found Wells Fargo had for years been buying a certain kind of auto insurance from National General Insurance and applying it to auto loan customers’ accounts without their knowledge.

Those borrowers were charged interest not just on their loans but on the insurance premiums as well, pushing more than 270,000 of them into delinquency.

Wells Fargo, the nation’s fourth-biggest bank, said it had already committed to using most of the settlement amount as part of the plan it developed to repair the damage its customers had suffered under the practice. National General Insurance, which according to the report paid Wells Fargo unearned commissions on the insurance policies, will pay $7.5 million as part of the settlement, bringing the total settlement amount to just under $400 million.