refactored triple-nested loop into explore_words method