added condition to make sure that connections are successful