Refactored to better support database availability interruptions.