The thermodynamics of the removal of heavy metals from wastewater and sludge