Wells Fargo is retreating from the Midwest.

The bank, scrambling to cut costs and dealing with a wave of scandals, announced plans Tuesday to sell all of its branches in Indiana, Michigan and Ohio. Wells Fargo, the second-largest bank in the country, will no longer have a retail presence in those states.

Flagstar Bancorp (FBC), a savings and loan, is buying the branches — 52 in all, including four of Wells Fargo's locations in Wisconsin.

All told, about $2.3 billion in deposits will leave Wells Fargo. The company said that all 490 employees will get offers to work at Flagstar.

"We remain committed to these communities," Mary Mack, Wells Fargo's head of community banking, said in a statement. Mack said Wells Fargo (WFC) will "continue to have a presence in the area" through commercial lending, wealth management and mortgage lending.

The sale includes 33 branches in Indiana, 14 in Michigan and Wells Fargo's only branch in Ohio, plus the four in Wisconsin. Wells Fargo will still have 48 branches in Wisconsin.

It comes as Wall Street pressures Wells Fargo to slash costs and as the bank grapples with soaring legal bills linked to the fake-accounts scandal and its many other troubles.

Wells Fargo detailed plans in January to pull the plug on more than 800 branches by 2020 as more Americans do their banking online. That will leave the company with about 5,000 locations.

Related: Wells Fargo altered documents about business clients

In February, the Federal Reserve hit Wells Fargo with unprecedented sanctions for "widespread consumer abuses." The Fed will prevent Wells from growing beyond $2 trillion in assets until it shows it has cleaned up its act.

Wells Fargo recently indicated that it has more work to do to satisfy regulators and that the penalties are likely to remain in place until next year.

A Wells Fargo spokesperson said the sale of branches in the Midwest has "no connection" to the Fed's sanctions.

Flagstar said it agreed to pay a 7% premium for the deposits it is acquiring. That implies a purchase price of just under $2.5 billion. The deal is expected to close in the fourth quarter.

The acquisition will more than double the customer base of Flagstar, which is just a fraction the size of Wells Fargo. The Michigan-based bank is valued at $2 billion and has about 3,500 employees. By comparison, Wells Fargo has a $267 billion market value and employs about 263,000 people.

Flagstar said the branches will operate as Wells Fargo locations until the deal is closed. After that, they will be rebranded as Flagstar. The bank said it plans to retain all branches and employees "at closing."

Related: Wells Fargo, Uber and Facebook: 3 corporate apology ads

Wells Fargo has been reeling from scandals for nearly two years.

The bank has admitted its workers opened as many as 3.5 million fake accounts, and charged customers for auto insurance they didn't need and mortgage fees they didn't deserve. Wells Fargo has also been accused of ripping off mom-and-pop shops on credit card fees.

More recently, Wells Fargo discovered some workers improperly altered documents about business customers in 2017 and early 2018. Wells Fargo also agreed last month to pay $480 million to put to rest claims that it misled shareholders about the fake-accounts scandal.

— Correction: The headline on a previous version of this story incorrectly said Wells Fargo was closing the branches. The story also misstated the asset limit the Fed has placed on Wells Fargo.