Yet more code cleanup/optimization. Please let me know if any of this breaks the script.