fixed memory freeing bug