Initial import of (slightly modified) script from Ben Ubois's repo.