fix segfault, fix all memory leaks