Port to FreeBSD

Port to FreeBSD and some cleanups.