Try to fix failed queries and dupliquates queries to the server

Hi, the second commit is probably not the right thing to do, i have found no other way to have one query per click.
What if we put lines 165-170 into a new function like `loadPage`? This logic is repeated in gotoPage as well.