cleaned up selection function to make it easier to understand