Memory management refactoring