Refactored database connection code into dbmanager.py and added more exception handling.