fixed a segfault and a memory leak