Clean up closing code a bit.