The Supreme Court ruled Friday in favor of an immigrant who faced deportation after receiving bad legal advice, setting a precedent that will help people who plead guilty to a crime without being made aware of the consequences, and then find themselves at risk of being kicked out of the U.S.

Chief Justice John Roberts wrote the majority opinion in the court’s 6-2 ruling, which throws out a drug conviction against South Korean immigrant Jae Lee, who came to the U.S. legally with his parents in 1982 at age 13. He was running a Chinese restaurant in Memphis in 2008 when he was caught with 88 ecstasy pills, $32,432 in cash, and a loaded rifle.

Lee eventually copped a plea deal in exchange for a lighter sentence, but only because his lawyer repeatedly assured him it was not a deportable offense.

The attorney was wrong. Lee had a green card, but the drug charge counted as an “aggravated felony,” which triggers mandatory deportation. Lee moved to have his conviction overturned, arguing that he received “constitutionally ineffective” counsel. He admitted that the drugs were his but claimed he would have pursued a “Hail Mary” trial in hopes of winning an acquittal and avoiding deportation.

Two lower courts ruled against Lee; on Friday, the Supreme Court reversed those decisions. “But for his attorney’s incompetence,” the chief justice wrote, “Lee would certainly have known that accepting the plea agreement would certainly lead to deportation.”

Justice Clarence Thomas wrote the dissenting opinion in Jae Lee v. United States, arguing that the Constitution “does not require counsel to provide accurate advice” about a guilty plea triggering deportation.

Immigrant rights advocates, meanwhile, cheered the decision, noting that immigrants often can’t afford to hire attorneys who are experts in both criminal defense and immigration law.

“The result is defense attorneys advising clients on the impact of laws they don’t understand and don’t have the time to learn,” said Ames Grawert, counsel at New York University Law School’s Brennan Center for Justice. He noted that mistakes happen “often” and said it’s “common” for criminal defendants to get bad immigration advice.

“Guilty pleas are supposed to be knowing, voluntary, reasonable,” Grawert said. “Taking a plea without knowing the immigration consequences is anything but that.”

Despite his Supreme Court victory, Lee could still be deported if prosecutors decide to refile the drug charges and seek a conviction at trial.