query management: refactor code to be human readable