Add null check for window object