reconstructed the presence code and added connection failure handling