This commit *should* fix these problems for good. It seems that my attempts at optimising the code (by exiting a loop once the first match was found) were causing it to fail.
I've also made the code aware of handle swapping. I noticed that the handles were not being corrected appropriately after negative scaling.