Minor optimizations of fold's query generation