fixed uninitialized attach_files
debug messages only in testmode
added debugmode, cleaned up and fixed some logic