Miscellaneous bug fixes; begin  debug statements to functions