Don't sleep to wait for reconnecting, use a timer instead.
Remove excess sleep and env setting.