First committed version, nearly identical to the TG scripts.