Fixed authentication bug and added code to check QueryFailure flag.