Alexeis patches for better handling of collect_common_factors.