Add setTimeout to prevent race condition