fixed some problems found by valgrind