New implementation of containsLoop().