Refactored queue selection code.