Patch contributed by Anders Carlsson<andersca@mac.com>.