Minor portability improvements

Obtained from FreeBSD port