fixed memleak and segfault