fixing heap bugs, lots of debugging code