Refactored the code to fetch categories only when the variable is null