Replace spaces with underscores in group names
Fix ∞ loop and possibly reversed if statement