Replace host matching and code cleanup