Improve hightlight query code: make cursor abstract, tidy up code