Bugfix/random failures

Tested with a while loop for a few hundreds of iterations (had to break).
This is a fix for #41.