Replaced all-at-once pattern match with an inner loop over each variable.
The method drops dependencies if they have been seen previously.