Finally fix reconnection logic

Really, that part of the code needs a good old refactoring.