major memory management refactoring