fixed memory leaks and removed ALL loops added comments