This file is pretty complicated and I don't have the time to check it very exhaustively. But fortunately this functionality has a lot of unit tests. I'll check any changes to the tests thoroughly and hopefully if they pass it means this all works.