Fix FFI types warning and some bugs uncovered