Fixing the infinite loop