Better debug output, no more chdir, all before some query changes
this may work better