Fix segfault and memory leak