Added some changes from Steve Bennett's patch for OS X