I'd suggest moving some of the existing code into helper functions first. It's already hard to follow and these changes to it aren't making it any simpler.
Also, ten commits per MR is rather too many (especially when they're non trivial). Try sending the first five or so. If you want to show where it's going, then include a link to a branch which contains the finished set.