Refactor queue algorithms