Bank of America and the U.S. Justice Department have agreed on the largest settlement ever reached between the government and a company, the bank announced Thursday. The $16.7 billion settlement covers toxic mortgages issued during the housing boom by its Countrywide Financial unit in Calabasas and other mortgage lenders it acquired last decade. The Charlotte, N.C. bank said the claims relate primarily to Countrywide and Merrill Lynch mortgages issued prior to Bank of America’s acquisitions. Countrywide originated or bought about $1.4 trillion in mortgages between 2005 and 2007. Bank of America acquired the firm in 2008. As part of the settlement, Bank of America will pay a total of $9.65 billion in cash to the government and provide about $7 billion worth of consumer relief. The cash consists of a $5 billion civil monetary penalty and $4.63 billion in compensatory remediation payments. The $7 billion in borrower relief will be in the form of mortgage modifications and community reinvestment and neighborhood stabilization efforts, with initiatives focused on communities experiencing, or at risk of, urban blight. Also, the bank will support the expansion of available affordable rental housing. It has committed to complete delivery of the consumer relief by no later than Aug. 31, 2018, and it will be subject to oversight by an independent monitor. “We believe this settlement, which resolves significant remaining mortgage-related exposures, is in the best interests of our shareholders, and allows us to continue to focus on the future,” said Bank of America Chief Executive Brian Moynihan, in a prepared statement. The settlement comes as the federal government has sought to crack down on banks that performed irresponsibly leading up to the financial meltdown of 2008. Bank of America inherited the legal liability through its acquisitions. The banks are accused of issuing mortgages that borrowers were unable to pay, and then selling the loans in securitized packages to investors. The Bank of America deal tops the $13 billion deal between the U.S. and J.P. Morgan Chase late last year, making it the largest to date. The settlement covers claims by the Department of Justice, the Securities and Exchange Commission and State Attorneys General from California, Delaware, Illinois, Kentucky, Maryland and New York. The settlement follows a $1.3 billion judgment late last month levied by U.S. District Judge Jed Rakoff in New York, who ruled Bank of America was liable for loans sold by Countrywide prior to its acquisition. At the center of that decision was the Countrywide program known in the industry as “The Hustle,” which focused on incentivizing mortgage loan officers to churn out mortgages quickly, not really considering the mortgage holders’ ability to repay the loans. The fine was based on the amount of losses incurred by investors in the loans, including government-sponsored mortgage operations Fannie Mae and Freddie Mac. Including a previous settlement with Fannie and Freddie of nearly $6 billion, the total tab for Bank of America stands at nearly $24 billion.