queue implementation and refactoring