Thanks ohrenkino!
I must have copied the parts wrong when I replied. Thanks for catching the error.
Your code is certainly more optimized and overall much easier to read! It hadn't occurred to me to use $IF statements in the body of the code like you did.