added code to prevent unnecessary connections