fdo#73001: Simplify the selection function logic & calculate correct results.

Fixing a bug and cleaning up the code all at the same time.  And don't forget
to write test for it as well.

Change-Id: Ia0322c4bebd4c5debcbfa4bb0902afbe581208b2