fixed bugs when memory leaks, added commenting