Nicola Sturgeon has said she is "obsessed" with keeping the SNP in power and not ending up in an "existential crisis" like Scottish Labour.
The First Minister said watching Labour go from being "impregnable" in Scotland to their current position as the third-largest party in the Parliament has profoundly influenced her leadership.
Speaking to political comedian Matt Forde at the Edinburgh Festival Fringe, Sturgeon reflected on a time when she would joke about weighing rather than counting Labour's votes at elections.
She said: "I've seen Labour go from way up there - a seemingly impregnable position - to where they are right now, in single figures at elections and in an existential crisis.
"It's kind of hard-wired into me, it's in my DNA to avoid making the mistakes that I saw them make at every turn."
"I'm obsessed," she said, revealing she is "constantly assessing and reassessing what we are doing".
Sturgeon, who in 2015 led the SNP as they won 56 out of 59 Scottish constituencies, described the feat as "an unprecedented and probably unrepeatable record historic performance" but warned "you can become a victim of your own success".
Labour's "completely disjointed and really acrimonious relationship" between the party's MSPs and MPs was a factor in their decline in Scotland, Sturgeon argued, although she admitted there were sometimes "tensions" between the SNP at Holyrood and Westminster.
Sturgeon said: "I've got a very, very close relationship with Ian Blackford, our leader down there, which is essential because we talk all the time about their tactics.
"Inevitably two groups in two different parliaments will have issues that cause a bit of tension that we have to work through carefully but overall it works really well."
During the wide-ranging interview, Sturgeon also said she has a problem with the "nationalist" associations of the SNP's name and denounced those with extreme nationalist views, saying they are not welcome in her party.
"I have some problems with that word because of the global connotations of it but first and foremost I'm an internationalist - I want Scotland to be independent to give us more opportunity to chart our own course domestically but also so that Scotland can co-operate and play a bigger role with other countries on the world stage," she said.
"The SNP today is the most pro-immigration party in the UK. My nationalism is rooted in a desire to make the country I live in as good as it can be.
"I don't care where you come from, if you want to live in Scotland and consider yourself Scottish that's fine by me."
The First Minister spoke about so-called cybernats and said there was an "unrealistic expectation" for political leaders to "police" supporters' behaviour and comments on social media.
She said: "I'm not responsible for everything people say on Twitter, thank God.
"But it cuts both ways - I try not to look at it but some of the abuse I get on Twitter would literally make your hair curl.
"It's horrible, misogynist, really filthy, disgusting stuff."
Asked about inadvertently inspiring people who are racist, Sturgeon said: "Any party, any movement - regardless of what they stand for - will attract people you don't want."
Referencing a pro-independence banner often displayed in Edinburgh calling for 'England to get out of Scotland', she said: "The person with that banner does not speak for the SNP. That kind of sentiment has no place in Scotland."
She added: "You can be absolutely vehement and resolute about calling it out and saying very clearly to people: 'If that's your opinion, you don't belong in this party and we don't want you.'
"People who put up banners like that, I don't want them in the SNP.
"The most important thing for a person in my position is not to pretend that my party will never attract people with those kind of opinions but to be absolutely clear that we will never make the SNP a comfortable or welcoming place for them."