Fixing memory/garbage collection issues. Added commenting