Fixed some issues found by Valgrind.