added linked list loop detection