Test version with debug statements.  Also moved some common code out to a separate file.