Adding all this debugging code which turned out to amount to nothing helpful, but I'll upload it just in case I want it again..