Add handling to properly close MongoDB connections when finished.