Smarter and more general implementation of the comparator struct