LURKING in the eerie closes, the crowded, stinking tenements and along the Old Town's dark, damp wynds, petty crime, debauchery and all forms of human brutality festered. Pilferers, pickpockets, prostitutes, vagrants, robbers and burglars. And, very often, murderers.
It was Edinburgh in the mid-19th century, the birthplace of the Enlightenment, a respected seat of learning and home to the cream of Scottish society.
But, as Irish farmer's son James McLevy quickly discovered, Scotland's capital was also a rich melting pot for its vile, lawless dregs. So it was just as well that he was on the case.
Officially named on Edinburgh City Police's payroll records as their 'number 1' detective in a team of six, over three decades McLevy was involved in around 3000 cases.
McLevy's name went on to become famed in the annals of police history, thanks to his own diaries recording in vivid, sometimes caustic and often humorous detail the crimes, the people and the city streets where he worked.
Edinburgh's criminals were said to have fled at the sound of his giant footsteps, but it was his unique detection methods - one moment rubbing shoulders with the criminal fraternity, the next probing the educated minds of Edinburgh University's professors using scientific advances to develop clues - that sealed his reputation.
He might have been forgotten had he not published - in the 1860s - his account of his investigations.
And now his name and his position at the heart of Edinburgh's budding police force is - perhaps ironically for a man said to have been the inspiration for Sherlock Holmes - providing the heart and soul for yet another Edinburgh fictional crimefighter.
McLevy's name has already been loaned to a BBC Radio 4 crimefighting character, loosely based on his 150-year-old writings but given a dramatic edge by actor and author David Ashton. Currently in its fifth series - "quite an accomplishment for radio," declares Ashton - he has now moved into print to with the Inspector McLevy Mystery novels. Eventually it's hoped that tales of the crimefighter, born in County Armagh, who trained as a linen weaver and came to Edinburgh to work in the trade, could make the transition to TV - perhaps even played by the actor who has brought the radio character to life, Hollywood-based star Brian Cox.
It's a remarkable revival for a detective whose writings were adored by Victorian readers. Even back then, it seems, everyone loved tales of a good Edinburgh detective.
"I was doing research for a television play about Conan Doyle and came across a passing mention of James McLevy," recalls David. "I asked at the British Library and after what seemed like a couple of hours this book appeared, a sorry looking thing, falling to pieces and tied up with a piece of dingy ribbon.
"I opened it up and it was like entering another world. Here was this person with this wild humour which I liked, a kind of grandiose quality, someone who really fancied himself as a philosopher with a big character.
"He was known as 'Jamie McLevy, the thief taker' and this idea of him being so proud of what he was doing struck me. I saw the kernel of a character."
While his two novels - Shadow of the Serpent was published last year, and the second, Fall From Grace, is about to come out - use McLevy's name and an outline of his character in a string of fictional episodes, the real McLevy was just as fascinating.
He pounded a beat that took in Edinburgh's Old Town slums and Leith crime hotspots, areas like Calton Hill and Princes Street which were popular with pickpockets, and the slums of the High Street and Canongate.
McLevy had decided the best way to solve crime was to mix with the pickpockets, prostitutes, body-snatchers and thieves, using the information some let slip to drive down the crime rate, and mingling with the population in plain clothes, undercover.
He used his ability to blend into a crowd to great effect to snare notorious pickpockets Holmes and Angus McKay. The pair had taken to following McLevy to his home in Old Fishmarket Close each night to ensure he was safely out of the way before they went out looking for victims - a tactic that didn't impress McLevy.
"And what made it worse was that they thought I was utterly ignorant of all this care taken of me," he wrote.
So, he hatched a plan, perhaps the first recorded example of a police sting.
Aware that one of Holmes' favourite tricks was to use a woman, who was known as The Swan, to sing in public to attract a crowd whose pockets he could then pilfer, McLevy waited until the thieves had followed him home.
Then, heavily disguised, he crept out to join the crowd as they enjoyed the music. As Holmes sneaked up, ready to dip McLevy's pockets, one of his colleagues nabbed him.
"The strains of The Swan were hushed; nor did she begin again; she was too much affected to be able to sing when her tender mate was in the claws of the eagles," wrote McLevy.
On another occasion, he revelled in what amounted to hero worship from women after snaring three child-strippers - a 19th century crime which saw well-dressed youngsters stripped naked in the streets by thieves who then sold the stolen clothing.
As for his love life, McLevy wasn't averse to becoming involved with female criminals.
Jean Brash, known as the Princess of Pickpockets and the Queen of Thieves, plied an alternative trade from a beehive-shaped brothel known as the Happy Land in Leith. McLevy was a regular, and she was one of the few criminals to outwit him.
Former police inspector John McGowan studied McLevy as part of a masters degree course.
"That he existed is without doubt," he says. "But just how much of what he wrote about was true, how much was anecdotal and how much had been changed to make the story... well, who knows?"
• Fall From Grace, An Inspector McLevy Mystery by David Ashton, is published by Polygon on June 14, price 9.99. David Ashton will discuss his novel on June 12 at 7pm at the National Library of Scotland, George IV Bridge.