fix timing problems and valgrind warnings