Merged from jeho@jeho.org--2005 (patch 46-50), mwolson@gnu.org--2006 (patch 14)