updated to use a linked list instead of queue