implemented sorting, added counters