updated Aaron's original quick script to be completely general. Not yet tested