Fixed several infinite loops.

Change-Id: I39f1b287f3775ddf09ab43afd0227de1fd83f364