Updated from 10:37 a.m. EST

Bank of America ( BAC) said Friday morning that it will acquire embattled mortgage lender Countrywide Financial ( CFC) for approximately $4 billion in an all-stock transaction .

The deal values Countrywide shares at $7.16 per share, a 40% premium to where shares were trading before a Wall Street Journal report broke Thursday, but below Countrywide's Thursday close of $7.75. Under the terms of the agreement, shareholders of Countrywide would receive 0.1822 of a share of Bank of America stock in exchange for each share of Countrywide. The deal is expected to close in the third quarter.

Moody's Investors Service warned it may cut its rating on BofA's credit on Friday. Fitch Ratings affirmed its negative outlook for BofA and placed Countrywide on ratings watch positive. Standard & Poor's also put Countrywide on credit watch positive, removing it from its credit watch negative list.

In afternoon trading, Countrywide was slumping nearly 17% to $6.46. Bank of America was down fractionally to $39.16.

Calabasas, Calif.,-based Countrywide has been beset by rumors of a Chapter 11 bankruptcy filing. BofA already owned a 16% stake in the firm.

On Wednesday, Countrywide revealed that foreclosures had doubled to 1.4% of unpaid principal at its key servicing units and that late payments were ratcheting up as well. The news gave investors the sense that matters for the company could get even worse than they'd already become since the subprime collapse began last year.

Countrywide Bailout's Good Biz for BofA

In the statement announcing the deal, BofA says it will benefit from Countrywide's broader mortgage capabilities, including its extensive retail, wholesale and correspondent distribution networks, and Countrywide will benefit from the stability of being part of BofA.

BofA's original purchase of a stake in the mortgage lender, which won it praise on Wall Street, provided the bank with a rich 7.25% yield on convertible preferred stock and the right to buy 111 million shares at a strike price of $18. But with Countrywide's share price precipitously dropping in value, the BofA purchase began to look more an more like a bad investment by the bank's CEO Kenneth Lewis.

Shares of Countrywide surged as much as 74% on the news Thursday, but closed up more than 51% to $7.75. BofA's stock closed up 1.5% $39.30.

Countrywide shares had plummeted as much as 47% earlier this week after the bankruptcy rumors, dragging many other mortgage lenders with it. After word of the potential BofA deal leaked, IndyMac Bancorp ( IMB), which slipped to a 52-week low of $3.95 Wednesday, closed Thursday up almost 23% to $5.78.

Government-sponsored entities Fannie Mae ( FNM) jumped 6.5% and Freddie Mac ( FRE) rose 2.7%. Banks with big mortgage origination businesses like Washington Mutual ( WM) -- which itself may now be an acquisition target -- and Wells Fargo ( WFC) climbed almost 15% and more than 3%, respectively.

Although a logical move for Lewis's BofA, which had a right to trump any bid for Countrywide as a part of its original stake purchase, acquiring the mortgage lender is fraught with challenges.

For one, Countrywide's business could continue to flounder for the next several years, because the high-margin business of originating loans to borrowers with questionable credit has all but disappeared. Moreover, Countrywide revealed on Wednesday that it still has a portfolio comprised of nearly 10% in such subprime-rated mortgages, which it is hard pressed to unload.

"Seems like BofA's throwing good money after bad," said Peter Cohan, president of consulting firm Peter Cohan & Associates on Thursday. "They're doubling down on a bad bet." The question is, Cohan continues, "are they buying more liabilities on a net basis or are they buying something that actually has some value?"

But for BofA, an acquisition should hardly be viewed as obligation, or, for that matter, an act of charity as the bank serves as savior to the U.S.'s mortgage market. The crown jewel of Countrywide's business, at this stage in the subprime-infused collapse in mortgages, is its servicing portfolio -- a very steady revenue business that has been coveted by distressed investors including Wilbur Ross.

BofA is getting a lender at a deep discount and, in the process, purchasing a platform that could prove invaluable over a five- to 10-year period.