added additional code to prevent unnecessary queries from being sent to the server.