added multipurpose debug flag, labeled some inputs, reduced some redundant code