Chemical production and wastewater treatment