fix several issues reported by valgrind