Refactored opening database sequence