Added keepAlive and auto reconnect function

and a little cleanup