Adds null checks to avoid extra iterations.