Add category exists check before using S_LAST_ROW