fixed an infinite loop bug