Added try catch to handle invalid queries