fix a bunch of memory issues