Fixed various compile warnings and valgrind hints