Reduced number of database connections

Changed from opening a database connection in every function to opening
one per page and passing the handle to functions.