added connection check, added debug