refactor reconnect to main loop