fixing infinite loop issue