Added group-by option. Slight code cleanups