Fixed memleak and segfault