Fixed several leaks and a segfault