Removes unnecessary terminals