Fixed deallocation bugs