Thousands of gay and bisexual men are to be formally pardoned after a new law was passed unanimously by the Scottish Parliament.
Consenting sexual activity between men over the age of 21 was only decriminalised in Scotland in 1981.
It was a further 20 years before the age of consent for gay men was lowered to 16.
The new law will pardon men convicted of having consensual sex with other men before it was decriminalised.
It will also enable men to apply to have convictions for same-sex sexual activity that is now legal removed from central criminal conviction records - with the Scottish government saying it expects about 25 men to do so over the next five years.
But it will not apply to behaviour that is still illegal today - for example rape or having sex with someone under the age of 16.
Why were gay men prosecuted?
Before the law changed, men were prosecuted for offences including consensual sexual activity in private, kissing another man in a public place, or just chatting up another man in a public place - which was known as "importuning".
Such behaviour was legal at the time between a man and a woman, and is legal today between two men.
The Equality Network charity estimates that the total number of these historical convictions in Scotland runs into thousands, and that there are hundreds of men alive today with such convictions on their records.
Sex between women was never criminalised in this way in Scotland, and the same rules applied to it as applied for sex between a man and a woman.
The Historical Sexual Offences (Pardons and Disregards) Bill had received strong cross-party support since being introduced by the Scottish government in November.
At the time, First Minister Nicola Sturgeon offered an "unequivocal apology" to men who had been "convicted as criminals, simply for loving another adult".
She said: "Those laws criminalised the act of loving another adult, they deterred people from being honest about their identity to family, friends, neighbours and colleagues.
"By sending a message from parliament that homosexuality was wrong, they encouraged rather than deterred homophobia and hate.
"Nothing that this parliament does can erase those injustices, but I do hope this apology, alongside our new legislation, can provide some comfort to those who endured those injustices."
What will the new law actually do?
The bill will give an automatic formal pardon to people, both living and dead, who were convicted of historical sexual offences where the conviction was for conduct which is now legal.
However, the pardon is a purely symbolic acknowledgement that the laws were discriminatory, and does not reverse the conviction.
Anyone with a historical conviction for same-sex sexual activity that is now legal would have to apply for a formal "disregard" in order to prevent the information being included in disclosure checks.
It would then be up to Scottish ministers to decide, based on the available evidence about the specific case, whether the disregard is granted.
A successful disregard application means the conviction will not appear on any background check carried out by Disclosure Scotland, and the person will be treated as not having committed the offence.
What was said in the debate?
MSPs from all parties spoke in favour of the bill, with Justice Secretary Michael Matheson hailing the "excellent cross party support" for the legislation.
Fellow minister Derek Mackay said the bill would not just seek to right past wrongs, but could help "set cultural norms".
He said MSPs could unite to say that "it is OK for gay and lesbian couples to walk with their partners down the street and not live in fear of being ridiculed, spat upon or attacked, which unfortunately still happens to this day".
Tory MSP Annie Wells said the bill would seek to address the "lingering impact discriminatory laws could have on someone's life", while Labour's Daniel Johnson said it was "an important step on the road to undoing the wrongs of history and building a more equal society"
Lib Dem Alex Cole-Hamilton said members were striking down "one of the last remnants of a more prejudiced era".
There was not complete consensus in the debate however, with Scottish Green co-convener Patrick Harvie slamming the "cowardice" of SNP MSP John Mason and other members for "quietly assenting" to the legislation while holding contrary personal beliefs.
Mr Mason responded that "people of many traditional faiths" had a "genuinely held belief" that gay sex was wrong.
What is the situation elsewhere in the UK?
About 49,000 gay and bisexual men convicted of now-abolished sexual offences in England, Wales and Northern Ireland received posthumous pardons in January last year.
Thousands of living men convicted over consensual same-sex relationships were also eligible to apply to have their convictions removed.
It followed the pardoning in 2013 of World War Two code-breaker Alan Turing, who had been convicted of gross indecency.
Private homosexual acts between men aged over 21 were decriminalised in England and Wales in 1967.
The Equality Network says the Scottish legislation does a better job than the equivalent law elsewhere in the UK as it provides an automatic pardon to people who are still alive, and also covers all of the old discriminatory offences.