fixing infinite loop problem