Refactoring and working on memory management