My version of Matt's cleanup patch from bug 5051. This makes