Combine no class shipping with class shipping

I have the following case which I’m trying to solve. Most of my items I’m selling on my WordPress site are no class specified and I use the following formula to calculate shipping: 10 + ( 9.99 * [qty] ) and another class specified shipping with the following formula: 20 + ( 19.99 * [qty] )
So, all works as it should: the no specified class has shipping 19.99 for one item, additional 9.99 for another item and so on. Same for the specified class. One item is 39.99 and every additional item is 19.99.

The problem comes when people add one item with no class specified shipping and another with class specified in the cart. The shipping cost would be: 59.98 which I want to tweak to 49.98. It should take into account the most expensive shipping (in this case the shipping with class specified) and reduce the other to 9.99 because it has already one item added into the cart, so it shouldn’t add 10 to overall shipping cost. Is there any simple way to do this? I guess it shouldn’t be hard, just not sure where to start or maybe there’s already a plugin for this?