The SNP leadership has narrowly faced down an internal rebellion over its bid to overturn the party's long-standing opposition to Nato.
In a tense vote at the SNP autumn conference in Perth, delegates voted 394 to 365 to reject an amendment by rebel MSPs and party members that would have reaffirmed the party's anti-Nato stance, against the wishes of First Minister Alex Salmond and defence spokesman Angus Robertson.
A further vote to remit the Nato U-turn back to SNP policymakers for further consideration was also narrowly voted down by 425 to 360. Mr Robertson's new pro-Nato policy was finally approved by 426 votes to 332 after half an hour of counting votes by hand.
The U-turn means that the SNP will now apply to keep Scotland in Nato if it is elected to lead the first post-independence government.
Party rebels led by MSP Jamie Hepburn and seven other MSPs tried in vain to quash the U-turn and reaffirm the party's support for non-Nato alliance Partnership for Peace. The SNP had opposed Nato for decades, previously arguing that it is a nuclear alliance at odds with the party's anti-nuclear stance, but that all changed today.
Mr Robertson insists that Scotland will only remain in Nato on the condition that it will be allowed to remove nuclear weapons from the Clyde.
The rebels fear that Scotland will face pressure to retain them if it remains in Nato.
Some defence experts and officials from the Ministry of Defence and the Foreign Office have warned that Nato membership cannot be taken for granted, suggesting that the Nato top brass may take exception to the SNP's anti-nuclear stance.
A former Nato secretary-general warned that an independent Scotland's membership of the organisation is "uncertain at best".
Lord Robertson of Port Ellen questioned whether membership of Nato would even be open to Scotland under the terms of the SNP's new policy.