Changes/fixes from Paul Kingtiger's packaged version.