modfied to use linked list to avoid array resizing