Added support for specifying multiple groups.

Apart from this, the script now checks existence of groups before adding
and fails if one or more of the groups mentioned don't exist.
Also, the code has been slightly refactored.