Hoist common code out of if statement.