Memory debugging

I've rebased this against master, which fixed the only remaining unexplained test failure. If no more have been introduced by the rebase, then we should be good to go.