trying to fix endless loop problem