Strapped for cash and on the verge of losing his house, British author Ben Aaronovitch decided that writing a book on magic cops would save him from bankruptcy.
Years before, in less dire straits, he had a wild thought while contemplating the television shows of his childhood, including the popular 1970s British police drama The Sweeney.
"I thought, what if Gandalf (the wizard in Lord Of The Rings) worked for The Sweeney?" says the 52-year-old over the telephone from his London home.
Thus began the Rivers Of London series, an urban fantasy police procedural now in its sixth installation, with a spin-off comics series. The first book, with the same name, was published in 2011.
Aaronovitch, who is married with a son in his 20s, went from managing the crime and science-fiction sections of a Waterstones bookstore to seeing his books fill its windows.
The hero of the series is Peter Grant, a sarcastic young constable in London's Metropolitan Police who discovers his knack for magic after he tries to take a witness statement from a ghost.
He becomes an apprentice wizard with the mysteriously ageless Inspector Thomas Nightingale, who trains him to deal with London's supernatural community - vampires who drink jazz instead of blood, legendary Chinese swordsmen and more.
In The Hanging Tree, released last November, the duo deal with the suspicious death of a young girl at an exclusive party in posh Hyde Park.
The book's title is drawn from the grim history of this now-upmarket part of London, where prisoners were once hanged en masse at a gallows called the Tyburn Tree.
Aaronovitch's brand of magic differs from that in Harry Potter, where only select individuals have magic powers.
"I felt that was quite Chosen One-ish and a bit elitist," he says. "So I decided that anyone could do magic, but that it was dangerous and hard work and, if you did too much of it, you would die. It's like learning the violin, if violin-playing could kill you."
The series has been noted for the ethnic diversity of its characters, something which Aaronovitch says is still not as common as it should be in genre fiction or other media, such as television.
"In some ways, we've come nowhere."
In his books, half the rivers of London are ruled by goddesses who manifest as young black women, descended from a Nigerian nurse who drowned in the River Thames.
I felt that was quite Chosen One-ish and a bit elitist. So I decided that anyone could do magic, but that it was dangerous and hard work and, if you did too much of it, you would die. It's like learning the violin, if violin-playing could kill you.
BEN AARONOVITCH on how his brand of magic in Rivers Of London differs from that in Harry Potter, where only select individuals have magic powers
Grant is mixed race - his father is a white jazz musician and his mother a cleaner from Sierra Leone. He and his Muslim colleague Sahra Guleed often face casual racism on the job.
Aaronovitch, a former television writer for shows such as Doctor Who, recalls at least two occasions when he rejected a TV deal for his books because the producers wanted to whitewash Grant.
The rights to the books were optioned by Feel Films in 2013, though a TV series has yet to be made.
"I live in a city that's 50 per cent non-white," says Aaronovitch, who is white with relatives of African and Caribbean descent and grew up in the mixed-race neighbourhood of Kentish Town in north London.
"The first all-white environment I worked in was the BBC. Publishing is exactly the same - 95 per cent white. They don't reflect the diversity of the country, let alone the city."
The United States editions of the first two books drew criticism for their cover art, which show the indeterminate silhouette of a man with a gun. Readers accused publisher Del Rey of blanking out the man's face to disguise the fact that he is of African descent.
"It was a terrible cover," says Aaronovitch, who was not consulted. "It was a textbook bit of passive racism."
Both the British and US editions of the series now have the same covers, which are based on British artist Stephen Walter's hand- drawn London map, The Island.
Stressing the multicultural nature of London has become all the more crucial in the face of rising xenophobia and anti-immigrant sentiment in Britain after Brexit, which Aaronovitch is a vocal opponent of.
"People get apocalyptic about Brexit," he says.
"But I refuse to follow this agenda that's been thrust on us by the likes of (British politician) Nigel Farage and the orange monster (Donald Trump). I'm not going to let it mess with me."
•The Hanging Tree ($29.95) is available at Books Kinokuniya.