Changed implementation of groupByPk logic.