Added Andrew Bartlett's fixes to my changes to his original patch (at the