DRY:ed the script up a bit