Bugfix/random failures

Tested with a while loop for a few hundreds of iterations (had to break).