Mac support

various changes to allow code to run on OSX, and to improve performance