Added mode finding function