Fix off-by-one error
Add missing include
Fix build for FreeBSD 10