fixed memory leaks and did some house keeping