Set a maximum for the number of tries possible to avoid infinite loop.