Changed start-up & ending code

Changed the code to open / close the cursor, and the database file.