Captain Marcus Fraser netted a stoppage-time winner as Ross County beat St Mirren 2-1 in Dingwall.
Ross Laidlaw's blunder appeared to have cost County victory as Tony Andreu's free-kick ended up in the back of the net in the 72nd minute.
Laidlaw allowed Andreu's shot to squirm through his legs after the substitute's effort had clipped the wall and bounced in front of the County goalkeeper.
The home side had opened the scoring in this Ladbrokes Premiership contest 10 minutes earlier when Ross Stewart struck a brilliant volley.
St Mirren had a goal disallowed and hit the frame of the goal twice through Kyle Magennis but there was worse to follow for Jim Goodwin's side as Fraser converted Michael Gardyne's cross from close range.
County handed a debut to on-loan Celtic midfielder Ewan Henderson but there was little goalmouth action for most of the first half at a windy Global Energy Stadium.
The home side lost Iain Vigurs to injury in the 36th minute, the midfielder replaced by Joe Chalmers after being caught late by Kyle McAllister, who was booked.
The game burst into life a minute later when Magennis drove forward and blasted a shot off the bar.
Saints continued their flurry of chances. Jon Obika was close to connecting from a tempting cross from Junior Morias, who soon had a shot blocked following Ryan Flynn's through-ball.
The visitors thought they had the lead two minutes before the break. Sean McLoughlin forced the ball towards goal after a goalmouth scramble and Obika – who was ahead of Laidlaw – got a needless touch on the line to send it into the net. There was no offside flag initially but the officials consulted and the goal was disallowed by referee Colin Steven.
County had the wind at their backs in the second half and they made a bright start.
Vaclav Hladky produced excellent saves from Harry Paton's strike and Stewart's header as the home team produced some good football.
Kirk Broadfoot came on for his first St Mirren appearance in 12 years, following his return to the club from Kilmarnock, after Gary McKenzie picked up a knock.
County took the lead after Foster's excellent cross ended up falling for Stewart at the back post. The forward took a touch before sending a volley into the opposite top corner.
Magennis came close again two minutes later when he cut inside from the left and bent a shot off the junction of post and bar.
Paton's foul on Paul McGinn gave Andreu the chance to shoot six minutes after the Frenchman came on and he was ultimately celebrating after Laidlaw appeared to make the save.
County pressed for a winner. Liam Fontaine had a header saved, Stewart nodded wide and Josh Mullin had a shot blocked. And their persistence finally paid off.