fix segfault
Added tests
fix segfault
fixed possible segv on btree freeing