Remove debugging output, consolidate some duplicated code.