Thoughts on abstracting some of these more complicated checks into private helper methods? That usually helps as a reminder of what the check is actually doing, and avoid nested `if`s.

e.g. somewhere below this:
```
private

def self.foo?(return_list, ref, opt) # use a better method name
  return_list.length < opt[:accumulate] && ref >= opt[:until_up_to]
end

def self.bar?(return_list, ref, opt)
  return_list[-1] != ref || opt[:include_duplicates]
end
```

then this block of code can just be:
```
if foo? && bar?
  count[0] += 1
  ...
```