Fix segfault
Add more tests