a better fix of valgrind related issues, relates to #153