sapH: added cost and did some small speedups reusing buffers better.