Fixes bug #3581

Fixes bug #3581 and the issue noticed by markstory