Cleaned up / simplified some of the close() code.