Fix bug of not able to connect to mongodb. And several improvements.