Fixed database code for events