Finally added auto-reconnect. Also fixed up some things that I should probably put in my actual script.