After running the scripts many times, had to add some more delay to make sure it is consistently giving valid results.