added conditional for infinite loop