Fixing infinite loop