Fix recursive function so as not to give nulls when there is no child category
Refactor to ternary operator