Fixed some memory leaks/invalid reads