(code review) fixed memory leaks & some codes