Fix document closing