added window existence check before including window events