Fix issues #3005 and #3009.

Fixes issue #3009 introduced by my previous pull request and issue #3005 introduced by commit 467d8137d.