Fix a bunch of portability issues