separated connection logic to new function
bug fix!
remove dead code

code commented out was not being used for anything, so why keep it?
added comments, reordered logic, remove dead code