Clean up code for allocating teams

Shorten code
Avoid excessive len() calls