added a limit to the number of trials