fixed memory leaks and memory corruption stuff all over the place