Updated for latest refs, and close the connection when done