fix bug in memory management
cleaner memory management