A sex offender struck again while on a community payback order after he was given an "incompetent" sentence.

David Barrie, who has now been jailed for just over a year, committed a sexual assault and targeted a 15-year-old after he was given a community payback order instead of a prison term in October 2014.

This was despite breaching strict conditions - imposed on him in the wake of two previous sexual offences -when he was caught repeatedly drinking with a 16-year-old girl in his flat despite being banned from contact with females under 17.

Sheriff Charles Macnair QC, handing down the community sentence at the time, said: "As custody doesn't change your behaviour ... I'm prepared to give you another chance in the hope that that will change your behaviour."

On Tuesday, that decision was reversed by sheriff Lorna Drummond QC, who ruled the sentence was legally "incompetent".

She said: "The court should have been jailing you then."

In the following 13 months he carried out a sexual assault by touching a woman while she slept, for which he was jailed for eight months in January, then breached the order again.

The latest offence involved surreptitiously taking the phone number of his friend's 15-year-old sister and sending her a string of texts, including one where he described himself as a "pure beast" before asking her to call him.

Barrie then deleted all record of the texts, again flouting the conditions imposed on him.

Fiscal depute Eilidh Robertson told Dundee Sheriff Court that Barrie's first conviction came after he was caught carrying out a sex act while watching pornography in a public street.

He was later also convicted of carrying out sex acts on a 17-year-old girl in front of a 13-year-old girl, which prompted police to apply for a sexual offences prevention order (SOPO) in a bid to curb his offending.

Ms Robertson said: "One of the conditions of the accused's order is that he must retain the history of the calls and texts messages on his mobile telephone.

"He is deemed a high-risk offender, offender management officers have attended at the accused's home address on a weekly basis since the imposition of the SOPO to inspect his telephone for evidence of any communication with females under the age of 17.

"The text messages he had exchanged with the girl were not found during any of these inspections, therefore the accused had deleted the evidence of his conversation prior to such an inspection.

"When he was charged with this offence he asked if he would be released or held in custody.

"He added 'it's not likely, I've got nothing to lose which means I'm at my most dangerous'."

Barrie, 34, a prisoner at HMP Perth, pleaded guilty on indictment to charges under the Sexual Offences Scotland Act.

Defence solicitor David Duncan said: "He knows there's a difficulty and that if he doesn't address it he'll simply incur longer and longer sentences of imprisonment."

Sheriff Drummond QC jailed Barrie for 12 and a half months.

She said: "You targeted a 15-year-old girl and had inappropriate conversations with her in breach of your order.

"It's the second time you have done so. Last time the court should have been jailing you. For the protection of the public, custody is inevitable now."