Warnings and leaks

Clean up build warnings and remove some memory leaks reported by valgrind. More leak fixes coming.