Oddly enough, my implementation is almost at the same level of complexity of 2.x. It may look a bit more readable as it was broken into multiple methods. Sadly, it is just a pice of logic with plenty of different cases that need to be accounted for. Do you have in mind any way to make this simpler without sacrificing most of the features?