Add a loop to wait for DOM update