poor attempt at looping for k groups