A construction worker imprisoned for nearly three years was cleared by an apologetic judge on Thursday after his accuser admitted she lied about being gang raped to make her friends feel sorry for her.

William McCaffrey hugged his lawyer when state Supreme Court Justice Richard Carruthers — who'd also presided over William McCaffrey's rape and kidnapping trial — threw out the conviction and dismissed the initial charges. DNA tests also had called the conviction into doubt.

"I've been waiting for this for a long time," the soft-spoken McCaffrey said outside court. "I'm just glad it's over."

McCaffrey, 32, has been free on $5,000 bail since September.

Biurny Peguero, then 22, originally said three men, led by McCaffrey, raped her at knifepoint after tricking her into getting into their car after a night out in 2005.

She testified at his trial and said at his 2006 sentencing that the "tragedy changed my life forever." He got a 20-year prison term.

Defense lawyer Glenn A. Garber later persuaded prosecutors to use new technology to retest DNA samples from an apparent bite mark on Peguero's arm.

The initial tests were inconclusive. The new ones showed the genetic material not only wasn't McCaffrey's but came from at least two women, apparently friends of Peguero's who fought with her.

Peguero confessed her lie to a priest and then to authorities this year. She claimed she was raped because she wanted her friends "to feel badly" for her, and then was afraid to back down from her story as the case continued, prosecutors said in court filings this fall.

Peguero, who now uses the name Biurny Gonzalez, pleaded guilty Monday to perjury. The 27-year-old faces up to seven years in prison at her sentencing, set for February.

Carruthers apologized, calling the case "a catastrophe" for both McCaffrey and the criminal justice system.

"I convey to you my personal regrets for having participated, though unknowingly, in the injustice," said Carruthers, who had called the supposed attack "disgusting" during McCaffrey's 2006 sentencing.

"Given the startling turn of events, I now retract what I said about you," Carruthers said, later adding, "I wish you well."