Fix valgrind errors

Clean up a few valgrind issues from invalid memory access and memory leaks