Sigh... "I would like a better way to write re-write this script."
While we do aim to provide insight on snippets, asking for help by posting it as a snippet is not the way to go about getting it... Posting a snippet is making your code readily accessible to the general public. Asking for expansion/help in the introduction is basically saying "I'm shot full of bullet holes but I can still hold water."
From the looks of your code I'd say your introduction says it all.