Second-half goals from veteran midfielders Iain Vigurs and Michael Gardyne secured Ross County's Scottish Premiership status as they came from behind to beat Motherwell 2-1.
Sam Foley's early goal, coupled with a strike by Kilmarnock at Hamilton moments later, put County into second-bottom spot and they were heading for the play-offs at half-time.
But Vigurs curled in a brilliant equaliser within four minutes of the restart and Gardyne netted 21 minutes into the second half as County got more than the point they needed to guarantee their safety.
The victory secured a major achievement for John Hughes, who took over on December 21 with the Highland side bottom of the table.
The former Hibernian manager was handed a contract until the end of the season and will now hold talks over his future.
Grabbing three wins from their last three league matches will put him in a strong position to earn a long-term deal.
Motherwell – without the injured Allan Campbell ahead of his departure – also had something to play for. They needed at least a point and possibly more to secure seventh spot and the extra prize money that comes with it.
They showed their intent from the start, with Christopher Long making a couple of positive bursts that were stopped illegally. And they took the lead inside seven minutes when Foley bundled home Mark O'Hara's low cross at the back post.
County twice came close to swift replies. Jordan White volleyed against the bar from 12 yards and Keith Watson saw a header inadvertently blocked on the line by Gardyne.
Stephen Kelly soon fluffed a chance and the visitors, who replaced the injured Carl Tremarco with Charlie Lakin, had to wait until first-half stoppage-time to get another. A good move gave Gardyne the chance to cut in from the left, but he blazed well over.
The Staggies were level soon enough. Former Motherwell midfielder Vigurs rode a tackle and made space to curl the ball into the top corner from 18 yards.
Kelly passed up another opportunity before the visitors took the lead after White beat substitute Steven Lawless in a tackle and slipped in Gardyne, who finished calmly as Liam Kelly came out.
Motherwell pushed for an equaliser but could not create any clear chances and they dropped to eighth behind St Mirren on goal difference.
Their first defeat in five matches ended the season on a downer but will not take away from the progress made since Graham Alexander took over in early January with his side level on points with bottom side County.