fix for connections not being closed correctly (needs to be refactored)