This code looks like an exact duplicate of the one before. Why not put it in a single place before `if (type)`?