Fix #1085 - Based on a patch from @codingfree