refactor to use deque