finally fixed the infinite loop