The previous code here was potentially more optimal for some cases, however, I never tested the perf, so it might not actually matter. This code handles more cases. We can go back and re-evaluate that other implementation if it seems worthwhile in the future.