fix the gen algorithm