Refactor to include logging database interaction