Restructured database engine connection code