Portability fixes

Mostly for Mac OSX