[[ Memory Leaks ]] Prevent some potential memory leaks
Merge pull request #1211 from runrevali/refactor

[[ Memory Leaks ]] Prevent some potential memory leaks