Fixed some problem spotted by valgrind.