cleaned up code/added comments/moved my query all function inside the if connected condition