Fix for bug 1005007 and bug 1006526