Adjust control flow to check empty vectors (#3988)