Fix segfault problems