Patch from Chris Moore to improve the generality of