More cleanup code and window close error checks.