updated code

added connection checking to script