Thanks to @agiletortoise and @sylumer for the help. I posted the finished script to the forum just now.