fix valgrind and unit tests