Adds a if condition to check the key if it's in the sequence in order to avoid StackOverflowException.