Add exponential backoff on connection errors
Move some functions around to make things a little cleaner