add waitForPageLoad and refactor a little.