Optimized the database controller to be more robust