Improve query_state::update(), fix locking

This hopefully fixes #8.