added control of number of restarts, fixes #41