Labour has gained the Dunfermline constituency in a Scottish Parliament by-election, beating the SNP by nearly 3,000 votes.
The by-election was triggered by the resignation of shamed politician Bill Walker, who was jailed last month for abusing his three ex-wives.
Winning candidate Cara Hilton said: "After the disgrace of Bill Walker, Dunfermline deserves better and I will ensure that we will be better - far better than what went before."
The SNP had taken the seat in the 2011 Scottish election, finishing with 590 more votes than Labour.
But Walker's actions meant the party was forced to defend the west Fife constituency mid-way through its second term in government.
The by-election, with a turnout of 42.75%, was declared shortly before 2am.
Ms Hilton, who was elected with 10,275 votes, said Labour was reconnecting with voters. And she claimed independence had been a turn-off among the electorate.
"The people of Dunfermline have rejected Scotland being put on pause for another year," she said.
"The government of Scotland has been suspended so that a referendum campaign can be won.
"We need a Scottish Government that will address the needs of Scots, not one that will simply make promises about what will happen after 2016.
"Today Dunfermline has sent a message to Bute House and Alex Salmond: it's time for you to focus on the real priorities of Scots, not your constitutional obsession.
"Use the powers you have now to make a difference, not just argue for more in the future."
She also praised the action of Walker's ex-wives, which triggered the electoral contest.
"We are only here tonight because of the bravery of three women," she said.
"Three women who came forward to demand justice and who won that fight.
"Their courage and determination must be a reminder to all of us that we need to work harder to ensure that all victims of domestic violence are able to come forward and receive the justice they deserve."
Although Walker had won the seat for the SNP in 2011, he resigned as an Independent MSP, having been kicked out of the SNP when the abuse allegations surfaced.
SNP candidate Shirley-Anne Somerville, who attracted 7,402 votes, said her party could remain proud of irs campaign, which focused on the threat of school closures in the area.
"This has been a long and interesting campaign for all of us," she said. "It is one that the Scottish National Party can be particularly proud of.
"We've run a positive campaign trying to support local parents in their schools and I hope we can come together, all of us in the party, to make sure those three schools in the Dunfermline constituency stay open."
She had been supported by First Minister Alex Salmond - the SNP leader - who took to the streets of the Fife town in a last push for votes last night.
The SNP also focused on its national policies such as the council tax freeze and the decision to remove the tolls on the nearby Forth road bridge.
The party's opponents argued that the SNP is centralising local services and spending too much time on the independence referendum.
Liberal Democrats have been successful in the past at Westminster and Holyrood levels but would have needed to persuade a huge number of people to back them this time around.
Candidate Susan Leslie, who picked up 2,852 votes, drew attention to the number of women standing in the contest.
"I think it has been a victory for women in politics in Scotland that four women stood in this by-election and fought positive campaigns on the issues for Dunfermline," she said.
Scottish Conservative candidate James Reekie said: "When the people of Dunfermline are faced with the dilemma of Labour and the SNP, they chose the Conservatives."
He received 2,009 votes, one percentage point up on the last election.
The by-election was also contested by Zara Kitson for the Scottish Green Party, who got 593 votes, Peter Adams for Ukip, with 908 votes, and John Black, an independent who received 161.
The results show a swing from SNP to Labour of nearly 7%. Ukip, Green and the Independent candidate all lost deposits.
A second by-election took place in the Dunfermline South ward of Fife Council, which was held by Labour.
He told BBC Radio Scotland that if the swing seen in Dunfermline were repeated in the Scottish elections, the SNP would still be the largest party at Holyrood.
"I think that puts the Dunfermline result into a context, much as though we would have wished to have won the seat," he said.