Based (loosely) on patch by Ner'zhul, DarkXuan, and [email protected]. Fixes issue #3429. --HG-- branch : trunk