Debug logging

I've left the pages function for the moment as it needs a bit of tidying