fixing memory leak caused segfault