solved the segfault problem, passed valgrind check