The trio of clients that got Deutsche Bank AG in regulatory trouble this week had a shared back story: They were all castoffs of JPMorgan Chase and Co.

Deutsche Bank moved millions of dollars across the globe for convicted sex offender Jeffrey Epstein in the past decade, and billions on behalf of international lenders Danske Bank A/S and FBME Bank Ltd. Along the way, the German bank missed or disregarded compliance red flags for years, New York’s Department of Financial Services said Tuesday in levying $150 million in penalties.

JPMorgan’s moves to offload those same clients years earlier may have helped it dodge a similar bullet. The biggest U.S. bank stepped away from handling money for FBME in 2009. It distanced from the others around 2013, the same year it undertook a broad purge of higher-risk clients from its correspondent banking business.

The bank had incentive to offload risky clients. JP Morgan’s primary U.S. regulator, the Office of the Comptroller of the Currency, put it on notice in early 2013, faulting its due diligence processes and ordering it to clean up its anti-money laundering controls. Later that year, the bank said it would spend $4 billion to shore up its compliance operations — a process that included reviewing the accounts of hundreds of clients and shedding many of them.

JPMorgan declined to comment.

Deutsche Bank, whose business is regulated in New York, said this week that it has cooperated with authorities and that it regretted bringing Epstein on as a client. Although it acknowledged deficiencies in its oversight of the FBME and Danske relationships, the bank said it found no intentional effort to facilitate unlawful activity. It declined to comment for this article.

Danske Flags

Concerns about Danske Bank, Denmark’s biggest bank, centered on its Estonia unit. News organizations including the Organized Crime Corruption and Reporting Project wrote in 2018 that the unit had helped rich Russians move nearly a quarter-trillion dollars from their country over the course of a decade, largely through anonymized shell corporations. The bank acknowledged later that year that many of those transactions should have been flagged as suspicious.

JPMorgan stopped providing the Estonia unit access to the U.S. financial system in 2013, citing the high percentage of client accounts domiciled outside the Baltic country. Deutsche Bank continued providing banking services to Danske for another two years.

The Justice Department is investigating the role played by Danske’s correspondent banks in the U.S., including Deutsche Bank, people familiar with the matter have said, adding that the authorities are also trying to understand JPMorgan’s role.

Deutsche Bank had a decades-long relationship with FBME, which is registered in Tanzania and based in Cyprus. In the 1980s, the small bank was a client of Banker’s Trust, which Deutsche Bank acquired the next decade. By 2005, Deutsche Bank had deemed FBME high on its risk scale, and its monitoring systems were flagging suspicious transactions at a rate of more than twice a week during some years, according to the New York regulator. Although Deutsche Bank admonished FBME at times over its practices and demanded more information about its clients, the German bank continued doing business with it, the regulator said.

JPMorgan started doing business with FBME in the early 2000s and quickly grew frustrated with its failure to fully explain certain transactions, according to documents reviewed by Bloomberg News. The banking giant was also wary of other practices at FBME, such as offering “hold mail” services, in which a bank allows clients to use the bank’s address to limit paper trails, according to those documents.

Deutsche Bank provided banking services to FBME for five years after JPMorgan stopped, processing some $618 billion in all. It ended the relationship in 2014, after the U.S. Treasury labeled FBME a “primary money laundering concern” for its alleged work with organized crime and terror groups, effectively freezing it from the U.S. financial system. FBME has disputed those allegations.

The late Epstein, for his part, was a longtime client of JPMorgan — characterized inside the bank as a “center of influence” who could help it attract lucrative clients. Epstein stayed with the bank well after 2008, when he pleaded guilty in Florida to soliciting minors for prostitution. Epstein moved over to Deutsche Bank in 2013, after a banker who’d recently joined from JPMorgan persuaded executives to bring him aboard, according to the DFS.

Epstein’s legal troubles mounted again in 2018. That November, the Miami Herald published a series of articles cataloging allegations from women who said they had been abused sexually by him as minors. Deutsche Bank notified Epstein in December 2018 that it would be closing his accounts.

More must-read finance coverage from Fortune: