Add condition to check if categories exist before looping through them.