Lots of cleanup and fixes for PHT