Better error handling for messed up queries