fix a bug causing infinite loop