Ported from Wizard's Infection script and optimized for more features.