fixing the infinite loop problem