Merged pdinh's code and debugging failures