Add safe-mode and distroyed window detection logic [Fix #64]