Quick'n'dirty port to FreeBSD (checked on FreeBSD 9.2/amd64)