fixed an infinite loop and a crash