Gloucester claimed their third successive Aviva Premiership Rugby victory after a dramatic late score denied a Nick Evans-inspired Harlequins at Kingsholm.
Brett Deacon touched down with three minutes left, just when it looked as though New Zealander Evans would make Gloucester pay for poor discipline.
Gloucester played half the second period with 14 men after props Paul Doran-Jones and Nick Wood were sin-binned, and their transgressions gave Quins a lifeline.
Fly-half Evans scored 21 points from a try, four penalties and two conversions, while England scrum-half Danny Care also touched down.
Evans' opposite number Nicky Robinson amassed 18 points, including conversions of tries from Dave Lewis, Charlie Sharples and Deacon, but Gloucester only just escaped from jail.
They led 26-13 early in the second-half, with a winning bonus point seemingly the most likely outcome of their dominance.
But the home side switched off as Doran-Jones and Wood both collected yellow cards, leaving Gloucester needing to dig deep during the final stages when they should have been comfortably clear.
England centre Mike Tindall returned to captain Gloucester after being rested for the 90-point stroll against Amlin Challenge Cup opponents Rovigo last weekend, but lock Jim Hamilton (ankle) and flanker Andy Hazell (illness) were late withdrawals, meaning starts for Dave Attwood and Luke Narraway, respectively.
Quins, without a win at Kingsholm since 1999, showed 10 changes from the side that crushed Challenge Cup visitors Cavalieri seven days ago, including starts for England pair Care and Nick Easter.
Robinson booted Gloucester into a fifth-minute lead, but Quins responded in opportunist fashion when Care caught the home defence cold.
He reacted quickest when the ball bobbled clear 10 metres inside his own half, and while several Gloucester players stopped in anticipation of referee Tim Wigglesworth's whistle, Care sprinted away with only Tindall embarking on a forlorn chase.
Evans added the extras, yet Gloucester replied just six minutes later through relentless pressure from their forwards that ended with Care's opposite number Lewis sniping over.
Robinson's conversion made it 10-7, and after he slotted two further penalties either side of an Evans strike, Gloucester added their second touchdown.
Sharples, twice denied by last-ditch tackling inside Quins' 22, made it third time lucky when he claimed overlap possession and finished impressively.
Quins were at sixes and sevens, finding themselves heavily outgunned up-front and also lacking pace out wide to cope with the twin threat of Sharples and James Simpson-Daniel.
Evans reduced the deficit by landing another penalty on the stroke of half-time, but Gloucester were good value for their 23-13 interval advantage.
Robinson quickly restored a 13-point lead with his fourth successful penalty, and Gloucester continued to show far greater urgency than Quins, sparked by Robinson's astute decision-making.
It was not all plain sailing for the home side though, as their Scotland international flanker Alasdair Strokosch limped off before Doran-Jones was sin-binned after a scuffle with his opposite number Joe Marler.
Marler escaped any punishment, but despite being temporarily reduced to 14 men, Gloucester continued to test an overworked Quins defence.
Quins knew they had to cash in on their numerical advantage, and Evans did exactly that, scything his way over from 25 metres for a try that he also converted.
Gloucester were suddenly under pressure and their problems intensified when just as Doran-Jones prepared to return, his fellow prop Wood was yellow-carded for tipping Easter out of a lineout.
Evans landed the resulting penalty, leaving Quins just 26-23 behind entering the final quarter of an exciting, if error-strewn, encounter.
Evans, who could do little wrong, then hauled Quins level with his fourth penalty, setting up a thrilling finale as Gloucester looked to reassert themselves on the contest.
And they finally came up trumps when their forwards rumbled over from close range and Deacon was accredited with the touchdown.