Adding database query logic