The highest court in Massachusetts has granted a new trial to a homeless man convicted of trespassing for taking shelter in privately-owned buildings during cold weather.

The Supreme Judicial Court found that the judge at David Magadini's trial was wrong to deny a request to instruct the jury on a so-called necessity defense. Magadini wanted to use that defense to argue that his behavior was justified for a person trying to escape extreme temperatures.

The court says the law "does not permit punishment of the homeless simply for being homeless."

The owners of three properties in Great Barrington had obtained no-trespass orders against Magadini. He was convicted of seven trespassing charges that occurred in 2014. The high court vacated six convictions but upheld one conviction for trespassing in June.