(code review) fixed few memory leaks and codes