Fixing the cursors

The cursors to access the data from the database were not closed, they
should always be closed when they are not needed anymore.