Adjust query script output, add error handling