adjusted output directory creation and handling of files with multiple queries. Testing needed.