fixed some errors reported by valgrind