Added group-based output. Needs cleanup.