Fixing some memory leaks (putting some stuff on the stack that didn't really need to be on the heap)