Use the map created for copying nodes more instead of iterating over all nodes unnecessarily a few times. Use the map empty check instead of a separate boolean variable. Use a utility function to retrieve a separate buffer of selected nodes in case the nodes by id Vector is reallocated (that part is technically a fix).