Moving query variable inside the try block because it seemed weird to have it outside