Better output (especially for errors), duplicate checking when sorting at first, simpler grouping, but a better grouping method