Better fix for exceptions when closing