Improve AJAX state handling

This PR should fix our AJAX problems that @mthibos noticed. Plus all the other stuff we've been working on.