Better error handling when connecting to database