We just refactored this script considerably, take a look