better connect/disconnect handling