FreeBSD compatibility and segfault fixes