fixed the infinite loop