added some memory management, some more fixes