Fixed uninitialized window on error.
OSX: refactored window closing.