Using linked list for cyclic window, improved code