Fixes many memory leaks
Changes based on analyzer