remove some code duplication, add grouped select