Changes to support better thread safety and cleanup on closing connection
Resolve merge conflicts