moved query recoding to utils, tidied up