#7 Refactoring and fixed leaks