merge duplicate code into abstract classes, and check for size 0 and 1 collections.