fixed the infinite loop problem