Attempting to fix the infinit loop (issue #39)

I had a hard time reproducing the error in issue #39, but this is an attempt to fix it. Again, can we have it tested to see if it is resolved?