Moved word handling to a function, added loop to handle word pairs for two word components.