Tidy up debugging code and document