Refactor queue functions