NEW YORK (AP) — A judge Thursday ordered President Donald Trump to pay $2 million to an array of charities as a fine for misusing his own charitable foundation to further his political and business interests.

New York state Judge Saliann Scarpulla imposed the penalty in connection with a lawsuit brought against Trump by the New York attorney general’s office over the handling of the Trump Foundation’s assets.

Among other things, the judge ruled that Trump improperly allowed his presidential campaign staff to work with the foundation in holding a fundraiser for veterans’ charities in the run-up to the 2016 Iowa caucuses. The event was designed “to further Mr. Trump’s political campaign,” Scarpulla said.

Last month, Trump’s lawyers and the attorney general’s office reached agreements to dissolve the foundation and distribute about $1.7 million in remaining funds to other nonprofits.

As part of those agreements, made public Thursday, Trump admitted personally misusing foundation funds. He and the attorney general’s office further agreed to leave it up to the judge to decide what penalty he should pay.

The settlement was an about-face for Trump. He and his lawyers have blasted the lawsuit as politically motivated, and he tweeted, “I won’t settle this case!” when it was filed in June 2018.

Trump’s fine and the charity’s funds will be split evenly among eight organizations, including City Meals-on-Wheels, the United Negro College Fund and the U.S. Holocaust Memorial Museum.

Attorney General Letitia James welcomed the resolution of the case as a “major victory in our efforts to protect charitable assets and hold accountable those who would abuse charities for personal gain.”

“No one is above the law — not a businessman, not a candidate for office, and not even the President of the United States,” said James, a Democrat.

The president admitted, among other things, to improperly arranging for the charity to pay $10,000 for a 6-foot portrait of him. He also agreed to pay back $11,525 in foundation funds that he spent on sports memorabilia and champagne at a charity gala.

Trump also agreed to restrictions on his involvement in other charitable organizations. His three eldest children, who were members of the foundation’s board, must undergo mandatory training on the duties of those who run charities.

Charities are barred from getting involved in political campaigns, but in weighing the Iowa fundraiser, Scarpulla gave Trump credit for making good on his pledge to give $2.8 million of the money raised to veterans’ organizations.

Instead of fining him that amount, as the attorney general’s office wanted, the judge trimmed it to $2 million and rejected a demand for punitive damages and interest.

The Trump Foundation said it was pleased by those decisions, claiming that the judge “recognized that every penny ever raised by the Trump Foundation has gone to help those most in need.”

Trump Foundation lawyer Alan Futerfas said the nonprofit has distributed approximately $19 million over the past decade, including $8.25 million of the president’s own money, to hundreds of charitable organizations.

At the time of the Iowa fundraiser, Trump was feuding with then-Fox News anchor Megyn Kelly and refusing to participate in the network’s final Republican presidential primary debate before the Iowa caucuses.

Instead, he held a rally at the same time as the debate at which he called on people to donate to veterans’ charities. The foundation acted as a pass-through for those contributions.

James said the evidence of banned coordination between campaign officials and the foundation included emails exchanged with then-Trump campaign manager Corey Lewandowski.

In one email, a Trump company vice president asked Lewandowski for guidance on precisely how to distribute the money raised.

Trump also admitted in the agreements to directing that $100,000 in foundation money be used to settle legal claims over an 80-foot flagpole he had built at his Mar-a-Lago resort in Palm Beach, Florida, instead of paying the expense out of his own pocket.

In addition, the foundation paid $158,000 to resolve a lawsuit over a prize for a hole-in-one contest at a Trump-owned golf course, and $5,000 for ads promoting Trump’s hotels in the programs for charitable events. Trump admitted these transactions were also improper.

As part of the settlement, Donald Trump Jr. reimbursed the Trump Foundation for the cost of the portrait.