why is this check needed?

(I am truly confused by the many if-else in this function)