Added a failure counter to avoid infinite loops.

This is related to #10.