Donald Trump allegedly avoided paying hundreds of millions of dollars in taxes some 20 years ago by accessing a questionable tax loophole that his lawyers said likely violated IRS rules and was later outlawed by Congress.

The Republican nominee, who has foregone a 40-year-old campaign tradition of presidential candidates making their tax returns public, has also boasted about the fact that he’s “smart” enough to have avoided paying any taxes for a number of years. But a new analysis of his Atlantic City bankruptcy filings conducted by The New York Times shows that Mr. Trump used a tactic that skirted not only ethics but also a legal line to avoid paying taxes on nearly $1 billion in canceled debt.

During the early 1990s, Trump built three casinos in Atlantic City by borrowing $1.3 billion. After just two years, the casinos filed for bankruptcy, leading Trump’s investors to forgive around $916 million in debt with the hope of keeping the operation afloat and receiving some return on their investments. While that $916 million in wiped-out debt should have counted as income in the eyes of the IRS, Trump used a “stock-for-debt swap” loophole, which allows companies to trade stocks for the remaining amount owed on a loan rather than paying taxes on the cancelled debt.

Trump’s casino ownerships operated as partnerships rather than under a company, which allowed him to profit directly on his personal tax return when business was booming. But when the casinos faced a downturn, Trump was able to swap “partnership equity” instead of paying taxes on his losses, similar to the way that companies swapped stocks. In these cases, the value of stocks or equity might be far from equivalent to what was owed in taxes.

The tactic was so unprecedented and questionable that Trump’s lawyers doubted its legitimacy, warning the businessman that the IRS would likely find it an overreaching exploitation of the loophole if he was audited.

“Due to the lack of definitive judicial or administrative authority,” his lawyers wrote in a letter at the time. “Substantial uncertainties exist with respect to many of the tax consequences of the plan.”

Congress outlawed the “stock-for-debt swap” provision in 1993, and extended that ban to partnership equity in 2004. Democratic candidate Hillary Clinton voted for the 2004 ban while serving as a New York senator. Trump has criticized Mrs. Clinton’s stint in the Senate, saying she failed to close tax loopholes that help billionaires.

As the nominee has declined to release his tax returns, it’s unknown whether or not the IRS ever questioned the maneuver, but it likely did save the real estate mogul’s casino empire.

Experts who have reviewed Trump’s filings say his controversial method prevented his investors from taking tax exemptions they could have claimed when forgiving the debt he owed.

“He deducted somebody else’s losses,” John L. Buckley, the former chief of staff for Congress’s Joint Committee on Taxation in 1993 and 1994, told the Times. “He is double dipping big time.”