Added some debugging messages.
Also moved the functions used to write debugging information into a more common place.