Changed implementation of collectTerms(...) to preserve order of terms. Before, it would group terms with the same constructor.