Minor fixes, improvements to debugging

This is still pretty messy, a lot of cleanup to be done
Fixes

Tightened up cache writing, improved debugging. Working pretty nicely now