Document and refactor query