change rank calculation to use avg instaed of sum
add connection closing