Error handling and connection to MongoDB