fixed memory leaks
fixes some other valgrid issues