Ian Rankin's famous world-weary detective John Rebus returns in his latest novel Midnight and Blue.
In the novel, Rebus, who has spent his life putting Edinburgh's criminals behind bars now incarcerated alongside them.
The first Rebus novel, Knots and Crosses was published in 1987 and this latest is the 25th in a series that has sold 30 million copies.
An unceasing quest for the perfect novel keeps him going, Rankin told RNZ's Sunday Morning.
Ian Rankin: My relationship with Rebus
"If I could just write the perfect novel, the perfect distillation of everything I want to say in the best possible way to say it, then I could stop and be happy. But no, every book is yet another slight failing on the road to writing the ideal novel," he said.
The 24th Rebus novel A Heart full of Headstones ends with Rebus about to be sentenced in court, it was, Rankin believed, a suitable place to end the Rebus story.
"I thought the previous book had would make a good ending to the series. It's basically Rebus' Reichenbach Falls. Sentence is about to be pronounced, the end. Let's just leave it at that.
"But fans vehemently disagreed. They wanted to know what happened in that courtroom, what was the outcome."
Having decided that Rebus would be found guilty and sent to prison, he became intrigued by the possibilities of that setting, he said.
"I thought, well that's an interesting situation, an ex-cop, an ex detective, in a prison surrounded by people who hate his guts because of what his job was, and some of whom he will have put inside.
"So, he's fearful, every day. He's nervous there could be someone coming up behind him with a with a knife or a weapon at any moment."
He then came up with the idea of a murder committed in a prison, one which was difficult to solve.
"Who you gonna call? There's one guy in that prison who is ready-made for solving the mystery, and that's John Rebus.
"So that was it, I was off and running."
The claustrophobic prison setting intrigued him, he said, its hierarchy and rituals a microcosm of society at large.
"And from page one in this book, I spell it out. Page one, Rebus is standing in a queue waiting for breakfast, and at the very head of the queue are the tough guys, the killers, the gangsters, or the ones you don't want to get on the wrong side of because they've got explosive temperaments.
"And then you've got the lifers, the people who've been in jail for a long time. They get a lot of respect, so they're next in the queue, and you work your way down to the weaker members of the prison population, and Rebus is somewhere down that pecking order."
His most famous character refuses to leave the building, he said.
"He's always lurking, waiting for me to come up with another story, another idea that will keep him breathing."
Rankin lives in the city where the Rebus novels are set with his wife and sons, but suspects rebus wouldn't love Edinburgh as he does.
"Rebus' Edinburgh does exist, and you don't have to look too far to find it, but it's also a beautiful city, and one of the problems for me is that Rebus doesn't see that because he's been a cop his whole adult life.
"For him, Rebus' Edinburgh is just a series of crime scenes waiting to happen. Whereas, if you're a visitor and you come to Edinburgh, it's an absolutely gorgeous city."
Rebus has been with Rankin since 25, when the sleuth, "sprang into my head, pretty much fully formed."
"And he just refused to go, he's a complex character. I often see him as an Old Testament kind of guy in that he sees the world in terms of absolutes, good and evil.
"Once you've committed the crime, you're always a criminal in Rebus' mind."
Putting him in jail therefore offered Rankin the possibility of challenging Rebus' jaundiced world view.
"One of the cons says to him, 'you're in here now, you're amongst us. Do you think we're all bad? Are we all bad men? Because when you were a cop, that's exactly what you thought.'
"So, I'm trying to change as a liberal, which I am, I'm trying to change Rebus' mind and tell him that there's nuances, that there's not just these polar absolutes of good and evil, but there are gradations and putting him in jail was a golden opportunity for me to try and change his mind."
Sign up for Ngā Pitopito Kōrero, a daily newsletter curated by our editors and delivered straight to your inbox every weekday.