This was the original gist the script came from.

I felt it could be reduced.