passed valgrind and clang static analyzer