Nightwatchman David Payne struck a career-best 62 to help Gloucestershire to a five-wicket victory over Glamorgan in their Division Two clash at Bristol.
Payne arrived at the crease on Friday evening following the dismissal of opener Chris Dent late on day three at the County Ground.
Resuming on 53-1 with 133 more required for victory, Payne put on 94 for the fourth wicket with Chris Taylor (39) after the hosts lost Richard Coughtrie and Kane Williamson to successive deliveries from Jim Allenby.
The left arm seamer beat his previous high score of 46 not out, striking 11 fours in his 103-ball innings before he was undone by spinner Robert Croft on the sixth ball after lunch.
Allenby trapped Taylor lbw three overs later to leave Gloucestershire on 162-5 but well within sight of victory.
The Gidman brothers, Alex and Will, wrapped things up with an unbroken stand of 26 for the hosts who took 21 points from the match, having been deducted one for a slow over rate.
An incredible 19 wickets tumbled at Bristol as Gloucestershire emerged from the madness needing a further 133 runs on the final day to beat Glamorgan.
Gloucestershire first collapsed from 278-2 to 341 all out in their first innings, but Jon Lewis and Will Gidman combined for seven wickets as the Welsh side were skittled out for just 148 to hold a lead of just 185.
Gloucestershire were left with 18 overs to bat before stumps and although they did lose Chris Dent in the penultimate over they still reached 53-1 and need just another 133 on the final day for the victory.
The pattern for the day was set early as Gloucestershire lost five wickets in the space of 41 balls for just 17 runs as their innings came crashing down around them.
New Zealander Kane Williamson, on 87, threw away the chance of his first Bristol hundred with a tame drive at left-arm spinner Dean Cosker, which gave James Harris a comfortable catch at mid-off.
Glamorgan held a slender lead, but could add just a further 148 runs as Lewis (4-43) and Gidman (3-31) steamed through the batting with just Mark Wallace (40) and Wil Bragg (36) offering any resistance.
Gloucestershire made the most of ideal batting conditions at Bristol to reach 278-2 by the close of day two in reply to Glamorgan's total of 378.
The visitors had resumed in the morning on 300-8 and saw their last two wickets add a further 78 thanks mainly to an unbeaten 70 from Graham Wagg, who hit nine fours and three sixes.
However Glamorgan's bowlers soon found it tough going when the home side started their reply, Chris Dent and Richard Coughtrie (50) putting on 103 for the first wicket.
Dent went on to make 81 but, with a second first-class century on the cards, the left-hander was run out by Wagg's throw from cover after being called through for a quick single.
The breakthrough allowed Kane Williamson to combine with Chris Taylor (42 not out) in an unbroken third-wicket alliance worth 111.
Williamson struck 12 fours in his 132-ball knock of 84 not out, helping Gloucestershire finish Thursday with a deficit of exactly 100 runs.
Half-centuries from Will Bragg and Jim Allenby helped Glamorgan to 300-8 on day one against Gloucestershire at Bristol.
Bragg hit 63 and Allenby 78 in a solid effort after the Welsh county had been sent in to bat.
Opener Nick James (40) and Bragg put on 103 for the second wicket and, later in the day, Allenby and Mark Wallace (42) combined for the same number of runs for the fifth wicket.
Gloucestershire hit back with four wickets in the final session, seam bowler David Payne finishing with 3-46.