Fix memory leaks
Refactoring
Add clang annotation