Major refactoring that makes memory system not recursive