Finish script and add query handling