Fixed some memory leaks and lowered memory usage