Refactored to handle multiple nodes.