Make it run under Mac OS X

Also refactor portability functionality into a library file