Numerous fixes for OSX

Need to test on Linux