patch by Harukalover, see bugtracker issue #736. Originally committed to SVN as r2244.
Originally committed to SVN as r1535.